【问题标题】:Express server not running when using ng serve使用 ng serve 时 Express 服务器未运行
【发布时间】:2019-07-09 12:06:24
【问题描述】:

当我从 angular-cli 使用 ng serve 命令时,我的 server.js 没有被执行。当我使用节点服务器时,它工作正常。

server.js:

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path');
const http = require('http');
const api = require('./server/routes/api');

const port = 3000;

const app = express();

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

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/api', api);

app.get('*', (req,res)=>{
  res.sendFile(path.join(__dirname, 'dist/Colab/index.html'));
});

const server = http.createServer(app);

server.listen(port, ()=>console.log(`Server is running on localhost:${port}`));

【问题讨论】:

    标签: mean-stack mean


    【解决方案1】:

    尝试使用以下命令:

      npm start
    

      node app.js
    

    【讨论】:

    • 我可以使用 node server.js,但是当我进行更改时,我必须重新构建应用程序并重新运行服务器。我想避免这种情况。 nodemon 是一个更好的选择吗?
    • 是的,当然,Ashish,这就是构建 nodemon 的目的。每当我使用 node 时,我总是使用 nodemon。
    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2019-01-07
    • 2018-11-08
    • 2015-09-28
    • 2019-06-23
    • 2018-02-03
    • 2022-07-20
    相关资源
    最近更新 更多