Express - 不能为不同的路由使用多个静态文件夹

时间:2017-12-09 13:29:23

标签: javascript node.js angular express routes

我试图通过Node / Express路线显示2个不同的Angular 2应用。 但我的路线只显示根的index.html,即使在其他路线的路径上也是如此。我尝试了很多不同的东西,但似乎没什么用。非常感谢任何帮助。

更新
我通过在 server.js

中替换这些代码解决了这个问题

但现在问题是我的路线网址显示为“http://localhost:3020/iot/#./”。我在app.module.ts中将LocationStrategy从 HashLocationStrategy 更改为 PathLocationStrategy 但是该路由不起作用。如何从我的网址中删除“#”标记。如果有人建议的话。

希望我的回答可以帮助遇到同样问题的人。

//View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.use('/', express.static(path.join(__dirname,'/client/dist/')));     
app.use('/mwc', express.static(path.join(__dirname,'/mwc/dist/')));   

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use('/', index);
app.use('/mwc', mwc);
app.use('/api/vi/', todos);

只有这个

app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use('/', express.static('client/dist'));
app.use('/mwc', express.static('mwc/dist'));
app.use('/iot', express.static('iot/dist'));
app.use('/canteen', express.static('canteen/dist'));
app.use('/api/vi/', todos);

&安培;包括我的角度应用'app.module.ts'中的以下代码&在index.html文件中将基础href从“/”更改为“./”。

import { CommonModule, APP_BASE_HREF, LocationStrategy, HashLocationStrategy} from '@angular/common';

providers: [
    { provide: APP_BASE_HREF, useValue: './' },
    { provide: LocationStrategy, useClass: HashLocationStrategy }
],

以下是上一段代码:

server.js

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var todos = require('./routes/todos');
var mwc = require('./routes/mwc');

var app = express();
var router = express.Router();


//View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.use('/', express.static(path.join(__dirname,'/client/dist/')));     
app.use('/mwc', express.static(path.join(__dirname,'/mwc/dist/')));   

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use('/', index);
app.use('/mwc', mwc);
app.use('/api/vi/', todos);

app.listen(3020, function(){
    console.log("Server started on port 3020...");
});

0 个答案:

没有答案