Node.js/Express将全部重定向到有角2页 [英] Node.js/Express redirect all to angular 2 page
本文介绍了Node.js/Express将全部重定向到有角2页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Node.js和Express创建Angular 2应用.
我遇到的问题是我的路线文件无法与通配符一起使用.每次我使用/
(例如/test
)以外的任何其他内容访问页面时,都会显示以下内容:ReferenceError: path is not defined
我的server.js:
I am creating an Angular 2 app with Node.js and Express.
The problem I am having is that my routes file doesnt work with a wildcard. Everytime I visit the page with anything other then /
(for example /test
) it says the following: ReferenceError: path is not defined
My 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:
My /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中要求路径包
You need to require the path package in your index.js too
/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;
这篇关于Node.js/Express将全部重定向到有角2页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文