Node.js / Express将全部redirect到angular度2页面
我正在用Node.js和Express创build一个Angular 2应用程序。
我遇到的问题是我的路线文件不能使用通配符。 每次我访问页面的其他东西/
(例如/test
)它说以下内容: ReferenceError: path is not defined
我的server.js:
const express = require('express'); const app = express(); const path = require('path'); const routes = require('./routes'); const data = require('./articles.json'); app.use(express.static(path.join(__dirname, '/dist'))); app.use('/', routes); app.listen(8080, function () { console.log('App started on port 8080'); });
我的/routes/index.js:
const routes = require('express').Router(); routes.get('*', function (req, res) { res.sendFile(path.join(__dirname + '/dist/index.html')); }); module.exports = routes;
那么我在这里做错了什么?
你也需要在你的index.js中需要path包
/routes/index.js
const path = require('path'); const routes = require('express').Router(); routes.get('*', function (req, res) { res.sendFile(path.join(__dirname + '/dist/index.html')); }); module.exports = routes;