NodeJS不提供静态html
在节点服务器上提供html文件时遇到问题 “index.js”中的代码如下所示:NodeJS不提供静态html,html,node.js,express,static,Html,Node.js,Express,Static,在节点服务器上提供html文件时遇到问题 “index.js”中的代码如下所示: const express = require('express'); const app = express(); var path = require('path'); app.use(express.static(path.join(__dirname + '/public'))); app.get('/', function (req, res) { res.sendFile('views/home
const express = require('express');
const app = express();
var path = require('path');
app.use(express.static(path.join(__dirname + '/public')));
app.get('/', function (req, res) {
res.sendFile('views/home.html');
});
app.listen(8081, function () {
console.log('Magic is happening on port 8081!');
});
以下是我的文件结构的图片:
views
|-home.html
public
|- some files here
index.js
运行服务器时,会出现以下错误:
TypeError: path must be absolute or specify root to res.sendFile
最后,我需要一个可以服务多个不同页面的服务器,这只是我服务的第一个页面(如果第一个页面不工作,就没有意义再继续下去了……)
我做错了什么?对于dir结构,您的代码必须是这样的
app.use(express.static(path.join(uu dirname,'public','views'),{index:'home.html'}));
app.use(express.static(path.join(uu dirname,'public'));
要使此代码正常工作:
const express = require('express');
const app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.sendFile('views/home.html');
});
app.listen(8081, function () {
console.log('Magic is happening on port 8081!');
});
您必须具有这样的目录结构:
views
|-home.html
public
|- some files here
index.js
文档和错误告诉您
sendFile
和[…]除非在选项对象中设置了根选项,否则路径必须是文件[…]的绝对路径。
()
所以你必须写:
res.sendFile(path.join(__dirname, 'public/views/home.html'))
运行此代码,将html文件保存在同一文件夹中,例如:index.html 在浏览器中,localhost:9000/index.html
var http = require("http");
var fs = require("fs");
var port = 9000;
//create server
http.createServer(function(req,resp){
reqUrl = req.url;
var day = reqUrl.slice(1,reqUrl.length-1);
file = reqUrl;
callPage(req,resp,file);
}).listen(port);
function callPage(req,resp,fileName){
fileName = reqUrl.slice(1,reqUrl.length);
console.log(fileName)
fs.readFile(fileName,function(err,html){
if(err){
throw err;
}
resp.writeHead(200,{"content-type":"text/html"});
resp.write(html);
resp.end();
});
}
以下是我的建议: 1) 将
视图
文件夹移动到与index.js
2) 更新index.js的代码
:
const
DEBUG_MODE = (process.env.DEBUG == 1),
express = require('express'),
app = express();
/* DEFINE VIEW RENDERER */
app.set('view cache', !DEBUG_MODE);
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
/* DEFINE VIEW FILES LOCATION */
app.set('views', __dirname + '/views');
/* KEEP STATIC FILES IN SOME CONSTANT (GROUPED) PLACE, EX.: /assets */
app.use('/assets/css', express.static(__dirname + '/public/css'));
app.use('/assets/images', express.static(__dirname + '/public/images'));
/* RENDER home VIEW FOR ROOT REEQUEST */
app.all('/', (req, res) => res.render('home'));
app.listen(8081, function () {
console.log('Magic is happening on port 8081!');
});
提供视图文件是express app的工作,只需调用res.render('view-name')代码>(无扩展,因为已在app.engine('html',…)中定义了cuz;
如果您需要示例:好的,所以我想出了一个解决方案
我将整个设置更改为这个
var express = require('express');
var app = express();
var port = process.env.PORT || 8081;
var path = require('path');
var http = require('http');
var router = express.Router();
//var data = require('./routes/data');
// Setting the view engine and defining paths
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
require('./routes/routes')(app);
app.listen(port, function () {
console.log('server running on port ' + port);
});
module.exports = app;
这是我现在想要的工作方式。不确定它是最佳的还是完美的,但我认为它能完成任务
谢谢大家的帮助!!我对node.js一无所知,但请尝试将路径更改为'/views/home.html'
@Chrisstar,当我这样做时,我会得到:Error:enoint:没有这样的文件或目录,stat'/views/home.html'。请添加目录结构的屏幕截图发生了什么事?您在控制台中看到错误了吗?浏览是什么ser show,什么状态代码?旁注:为什么要编写path.join(\uu dirname+'/public')
,它应该是path.join(\uu dirname,'public'))
这是可行的,但考虑到“home.html”页面之外还有其他几个页面,我觉得还有一种稍微不同的方法可以做到这一点。@henrikbossart您在问题中就是这样写的。您的问题不是如何解决其他几个页面的问题。但为什么您的代码不能满足给定的要求呢文件。对不起,我的错。我需要这个来工作好几天pages@henrikbossart它应该如何工作呢?当前yourdomain.tld/view/home.html
将为该目录或该目录中的任何其他文件提供服务器。如果这不是您想要的路径,请添加另一个express.static
,例如使用path.join(u dirname,'public/views/'))
因此路径应该是yourdomain.tld/home.html
。但是从这个问题来看,不清楚请求路径应该如何与所服务的文件相对应。结果是“无法获取/home.html”@henrikbossart如果您想要请求后端部分数据的前端部分,请制作两个应用程序:前端应用程序、后端api