项目目录结构是
school-data
|
+-- app
| |
| +-- app.js
| |
| +-- app.css
| |
| +-- server.js
| |
| +-- mini.js
| |
| +-- index.html
|
+-- home
|
+-- node_modules
| |
| |-- modules (like express)
和文件server.js
代码
var express = require("express");
var app = express();
app.use(express.static(__dirname + "/../public"));
app.get("/", function (req,res) {
res.send("server : i am working")
});
var server = app.listen(8081, function() {
console.log(new Date().toISOString() + ": server started on port 8081");
});
我正在尝试从浏览器访问http://localhost:8081/app/index.html并在http://localhost:8081正常工作时收到404错误
答案 0 :(得分:1)
即使你的目录结构不好,但这里的代码可以帮到你。
您应该按照以下
更新server.js
文件
var express = require("express");
var path = require("path");
var app = express();
//all routes start with /app will be redirected to app folder as public
app.use("/app", express.static(path.join(__dirname, '../app')));
app.get("/", function (req,res) {
res.send("server : i am working")
});
var server = app.listen(8081, function() {
console.log(new Date().toISOString() + ": server started on port 8081");
});