1

当我运行服务器时无法显示

/// Module dependencies.
var application_root = __dirname,
    express = require("express"), //Web framework
    path = require("path"), //Utilities for dealing with file paths
    mongoose = require('mongoose'); //MongoDB integration

//Create server
var app = express.createServer();

// Configure server
app.configure(function () {
    app.use(express.bodyParser()); //parses request body and populates req.body
    app.use(express.methodOverride()); //checks req.body for HTTP method overrides
    app.use(app.router); //perform route lookup based on url and HTTP method
    app.use(express.static(path.join(application_root, "public"))); //Where to serve static content
    app.use(express.errorHandler({ dumpExceptions:true, showStack:true })); //Show all errors in development
});

//Start server
app.listen(4711, function () {
    console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
4

2 回答 2

1

您需要在 public 中有一个 index.html 来提供站点 url。另外,如果您在静态之前使用路由(app.router)。确保它不使用“/”路由。哪个先出现,你会看到那个。

于 2013-10-17T09:03:38.673 回答
0

首先创建一个名为的文件夹public,然后在其中创建一个名为index.html.

你的最后一部分应该是这样的:

//Start server
let listener = app.listen(4711, function () {
        console.log("Express server listening on port %d in %s mode", listener.address().port, app.settings.env);

});

否则你会得到一个错误,app.address().portundefined.

于 2017-02-09T11:36:24.890 回答