【问题标题】:angular 2 file works directly directly but not via nodejs express fileangular 2 文件直接工作,但不能通过 nodejs express 文件
【发布时间】:2017-07-08 05:50:57
【问题描述】:

我正在启动一个 MEA2N 应用程序,并构建了一个在端口 3000 上运行的小型快速服务器。 角度应用程序在端口 4200 上运行。如果我打开 localhost 3000,我会看到“正在加载...”,因此应用程序无法运行。如果我打开 localhost 4000 我会得到“应用程序工作”。有人知道这是怎么发生的吗?

server.js 非常标准:

// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');

// Get our API routes
const api = require('./server/routes/api');

const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));

// Set our api routes
app.use('/api', api);

// Catch all other routes and return the index file
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, './src/index.html'));
});

/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '3000';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));

server.js 正在根文件夹中运行。 角度应用程序是 root/src/indxex.html 和 root/src/app/

【问题讨论】:

  • 请包含您的文件夹结构和您的 app.js

标签: node.js angular


【解决方案1】:

如果 server.js 位于 root 文件夹中,并且 Angular 应用位于 root/src/index.html

然后

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

应该是

app.use(express.static(path.join(__dirname, '/src')));

【讨论】:

  • 你是完全正确的。我已经改变了那部分,不幸的是仍然没有效果。打开 localhost 4200 有效。打开 localhost 3000 不会
  • @Mihaly 有什么错误吗?检查 chrome 开发人员工具上的网络选项卡,看看它是否可以找到 index.html
  • 不幸的是没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 2013-06-28
相关资源
最近更新 更多