【问题标题】:Node express server isn't working - how to create and start a server?Node express 服务器不工作 - 如何创建和启动服务器?
【发布时间】:2017-04-11 00:39:59
【问题描述】:

我正在尝试创建和启动服务器,但我一直在查看其他代码,但看不出为什么我的代码不起作用(只是得到这个:localhost 只是不断加载和加载,没有任何反应)。 有任何想法吗?谢谢!!!

应用程序/服务器/app.js:

'use strict'

var express= require ('express');
var path=require('path');
var bodyParser = require('body-parser');
var http = require('http');

var app= express();
module.exports = app;

app.use(express.static(path.join(__dirname, '../browser')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


var server = http.createServer();
  server.listen(1337, function () {
        console.log('Server is listening on port 1337!');
  });

app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send(err.message);

});

应用程序/浏览器/index.html:

<!DOCTYPE html>
<html lang="en">
 <head>
   <title>node</title>   
 </head>
 <body>
    <div>
     <p>Hey whats up</p>
    </div>
  </body>
</html>

【问题讨论】:

    标签: node.js http express server


    【解决方案1】:

    您的代码对我有用。

    您的代码中唯一的错误是您必须将 server.listen(1337, function ()..., 更改为 app.listen(1337, function () {...

    另外,我添加了一个文件路径...

    app.get('/', function(req, res){ res.sendFile(path.join(__dirname, '/index.html'));})

    ...将您的 localhost:1337 链接到您的 index.html 文件。现在您的 index.html 文件将在您转到 localhost:1337 时显示。

    最后,我不确定你是否需要这条线... var server = http.createServer();。我删除了它,一切正常。

    这是下面的代码。

    'use strict'
    
    var express= require ('express');
    var path=require('path');
    var bodyParser = require('body-parser');
    var http = require('http');
    
    var app= express();
    module.exports = app;
    
    app.use(express.static(path.join(__dirname, '../browser')));
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    
    app.get('/', function(req, res){
    	res.sendFile(path.join(__dirname, '/index.html'));
    })
    
      app.listen(1337, function () {
            console.log('Server is listening on port 1337!');
      });
    
    app.use(function (err, req, res, next) {
    console.error(err.stack);
    res.status(500).send(err.message);
    
    });

    【讨论】:

    • 谢谢它现在有效。正如您所说,当我将其更改为 app.listen 时,这一切都开始了。你能解释一下为什么 var server = http.createServer();和 server.listen 不起作用所以我明白了吗?正如我在教程中看到的,我首先必须创建一个服务器——通过: var server = http.createServer();然后我启动我的服务器 - 这就是为什么 server.listen (所以我启动了我之前创建的服务器。为什么这是错误的以及 - app.listen - 如何工作(如果我不创建服务器,我该如何启动通过app.listen?)。非常感谢!
    • 当然。所以我不确定这是否 100% 正确,但我相信 Express 有它自己的 HTTP 模块。我相信如果你不使用 Express,THEN 你会使用 var server = http.createServer(); 和 server。听,你以前的方式。但是由于您是使用 Express,它通过路由和其他功能使事情变得更容易,那么您可以使用 Express 来创建服务器。这就是您对代码 app.listen 所做的事情。因为 var app = express(),并且 app.listen 和 express().listen 是一样的,这意味着你正在创建使用 Express 的服务器
    • 谢谢。所以 app.list 都创建服务器并启动它,或者 var app=express() 自动创建一个服务器,然后我使用 app.listen 启动它?
    【解决方案2】:

    也许你写的端口 - 1337 正忙,通过选择其他端口检查,例如 4200 或 3000 - server.listen(4200,function () {

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2012-04-14
      • 1970-01-01
      相关资源
      最近更新 更多