[英]Node JS res.sendFile() not working
I'm getting ReferenceError: main is not defined
when I open up the http://localhost:3000/ 我收到ReferenceError: main is not defined
打开http:// localhost:3000 /时ReferenceError: main is not defined
Here I try to open up the main.html located in views directory 在这里,我尝试打开位于views目录中的main.html
This is my app.js 这是我的app.js
const express = require('express'),
app = express(),
config = require('./config/index'),
routes = require('./routes/route');
app.use(express.static(`${__dirname}/public`));
app.use(express.static(`${__dirname}/views`));
app.use('/',routes);
app.listen(config.port,()=>{
console.log(`Listing at port ${config.port}`)})
This is my route.js 这是我的route.js
const express = require('express'),
router = express.Router(),
helpers = require('../helpers/index');
router.route('/')
.get(helpers.index)
module.exports = router
This is my helpers/index.js 这是我的助手/ index.js
var user = require('../user/user');
exports.index = (req,res)=>{
if(user.name == ''){
res.sendFile(main.html);
}
else{
res.sendFile(chat.html)
}
}
module.exports = exports;
Directory Structure 目录结构
>helpers
>index.js
>routes
>route.js
>user
>user.js
>views
>main.html
>chat.html
app.js
pacakage.json
Change: 更改:
res.sendFile(main.html);
to: 至:
res.sendFile("main.html");
Without the quotes, it's trying to interpret main
as a Javascript object which it looks for the .html
property on. 没有引号,它试图将main
解释为Javascript对象,并在其中查找.html
属性。 But, there is apparently no object named main
so you get ReferenceError: main is not defined
. 但是,显然没有名为main
对象,因此您得到ReferenceError: main is not defined
。 You want to pass a string here instead. 您想改为在此处传递字符串。
Same for res.sendFile("chat.html");
与res.sendFile("chat.html");
If the files are not local to this module's directory, then you need to build a more complete path that specifies their location. 如果文件不是此模块目录的本地文件,则需要构建更完整的路径来指定它们的位置。 Given the file hierarchy you show, I think that might be something like this: 给定您显示的文件层次结构,我认为可能是这样的:
const path = require('path');
const options = {root: path.join(__dirname, "../views")};
res.sendFile("main.html", options);
In addition to jfriend00's answer, you must also, build the correct absolute path, using the global __dirname variable in node. 除了jfriend00的答案,您还必须使用node中的全局__dirname变量来构建正确的绝对路径。
So your path will be something like: res.sendFile(__dirname + "/main.html")
or, depending on your folder structure: res.sendFile(__dirname + "/someadditionalfoldrs/main.html")
因此,您的路径将类似于: res.sendFile(__dirname + "/main.html")
或,取决于您的文件夹结构: res.sendFile(__dirname + "/someadditionalfoldrs/main.html")
or, construct the path using "./" if applicable, like "./main.html"; 或者,使用“ ./”(如果适用)构建路径,例如“ ./main.html”;
var user = require('../user/user');
var path = require('path');
exports.index = (req,res)=>{
if(user.name == ''){
res.sendFile(path.resolve('views/main.html'));
}
else{
res.sendFile(path.resolve('views/chat.html'))
}
}
module.exports = exports;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.