首页 > 解决方案 > 从 Node 调用 Angular 会产生错误

问题描述

我正在尝试从节点调用 Angular,但在浏览器中出现以下错误。使用“节点服务器”时成功调用 Angular。

错误:

runtime.js:1 Uncaught SyntaxError: Unexpected token <
polyfills.js:1 Uncaught SyntaxError: Unexpected token <
styles.js:1 Uncaught SyntaxError: Unexpected token <
scripts.js:1 Uncaught SyntaxError: Unexpected token <
vendor.js:1 Uncaught SyntaxError: Unexpected token <
main.js:1 Uncaught SyntaxError: Unexpected token <

服务器.js

const path = require('path');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
process.env.INDEX = path.join(__dirname, '/dist/nghd01/index.html');

const configureMongoose = require('./backend/config/mongoose')
const configureExpress = require('./backend/config/express')

const app = configureExpress();

app.listen(4200);

express.js

module.exports = function() {
    const app = express();

    require('../app/routes/index.server.routes')(app);

    return app;
}

index.server.routes.js

const index = require('../controllers/index.server.controller');

module.exports = (app) => {
    app.get('/*', index.render);
};

index.server.controller.js

exports.render = (req, res) => {
    res.sendFile(process.env.INDEX)
}

标签: node.jsangularexpress

解决方案


我在 express.js 中遗漏了一行。我没有像这样指定静态文件的位置:

app.use(express.static(process.env.DIST))

推荐阅读