我正在使用Nodes.js快车做我的路线。
对于正常的那个..。本地主机:8080/订购它的工作井。但是,我尝试在ID方法localhost:8080/Order/ID-12345中使用get方法,并继续使用ViewOrder.html来执行该函数。但它显示出了一些错误。
错误
加载资源失败:服务器响应状态为404 (未找到) localhost:1337/Orders/img/p7.jpg
加载资源失败:服务器响应状态为404 (未找到) localhost:1337/Orders/css/bootstrap.min.css
等等..。
而不是localhost:1337/css/bootstrap.min.css
// Index Page
app.get('/Order', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/Orders/:item', function (req, res) {
res.sendFile(__dirname + '/ViewOrder.html');
});Order和Orders不是目录。我使用了代码路线的一部分
发布于 2017-06-15 09:52:40
是啊!您需要指定静态文件夹以服务静态文件。
所以,在你的情况下,我会对你的server.js这样做;)
var express = require('express'),
app = express(),
http = require('http').Server(app);
const ipaddress = "127.0.0.1";
const port = 1337;
app.use(express.static(__dirname + '/Order'));
app.use(express.static(__dirname + '/Orders'));
app.get('/', function (req, res) {
res.sendFile('index.html', {"root": __dirname});
});
app.get('/:id', function (req, res) {
var param=req.params.id;/// hu! gotcha little param
res.sendFile('Orders/index.html', {"root": __dirname});
});
http.listen(port, ipaddress, function () {
console.log((new Date()) + ' Server is listening on port' + port);
}); 根据注释(实际上,Order不是目录,我使用Order作为链接到目录的路由)
假装一个公用文件夹是您的根/静态--这将是我的第二次尝试
app.use(express.static(__dirname + '/public'));
app.get('/Order', function (req, res) {
res.sendFile('public/index.html', {"root": __dirname});
});
app.get('/Orders/:item', function (req, res) {
res.sendFile('public/ViewOrder.html', {"root": __dirname});
}); 注意
我不得不修改静态请求
<link rel="stylesheet" type="text/css" href="style.css">
- TO -
<link rel="stylesheet" type="text/css" href="/style.css">发布于 2017-06-15 08:53:58
在您的文件server.js (启动应用程序的文件)中,您是否设置了静态目录名?
对我来说,这解决了你的问题:
var app = express();
app.use('/Orders', express.static(__dirname));此设置为您的项目设置静力学文件。
有关更多信息,这是有用的表示静态资源
发布于 2017-06-15 08:49:36
如果您希望html页面加载css/js资产,则需要将它们作为静态文件(文档:http://expressjs.com/en/starter/static-files.html )提供。
https://stackoverflow.com/questions/44562781
复制相似问题