【问题标题】:Node.js/Express redirect all to angular 2 pageNode.js/Express 全部重定向到 Angular 2 页面
【发布时间】:2017-10-07 18:33:29
【问题描述】:

我正在使用 Node.js 和 Express 创建一个 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;

那么我在这里做错了什么?

【问题讨论】:

    标签: node.js angular express


    【解决方案1】:

    你的 index.js 中也需要路径包

    /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;
    

    【讨论】:

    • 天哪,谢谢。我将所有内容都移到了路由文件中,但忘记了路径。大声笑
    • @LuudvanKeulen 碰巧发生在我们当中 :-)
    • 我会在 8 分钟内接受它(所以计时器不会让我这么快接受它)。
    • @LuudvanKeulen 当然,不急 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2016-07-07
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多