我正在尝试运行我的第一个快递应用程序,但似乎无法让我的网页显示.我有以下代码:
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("files/config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
response.send("hello!");
});
app.listen(port, host);
console.log("Listening on port" + port);
Run Code Online (Sandbox Code Playgroud)
这是我的目录树
nodejs/
js/
javascript.js
public/
index.html
Run Code Online (Sandbox Code Playgroud)
我知道服务器正在运行,因为我"Hello!"在运行时在浏览器中收到了响应127.0.0.01:1337
但是当我尝试输入网页时1227.0.0.1:1337/index.html,我Cannot GET /index.html会在浏览器中显示
所以我猜测name我的get方法中的值有问题,但无法弄清楚它是什么以及如何修复它.
Joh*_*yHK 14
您的应用只会路由您在app.use(app.router)通话时设置的网页请求.因此,请将您的app.use呼叫重新排序为以下之一:
app.use(express.static(__dirname + "/public"));
app.use(app.router);
Run Code Online (Sandbox Code Playgroud)
__dirname是执行脚本所在的目录,因为它位于代码js对等的目录中,public需要:
app.use(express.static(__dirname + "/../public"));
app.use(app.router);
Run Code Online (Sandbox Code Playgroud)
Express 4 无需手动操作app.use(app.router).使用Express 4,您只需要:
app.use(express.static(__dirname + "/../public"));
| 归档时间: |
|
| 查看次数: |
24706 次 |
| 最近记录: |