[英]JavaScript (Node.js) : app.set('port', port); ^ TypeError: app.set is not a function
[英]I get error in node js in app.js file in app.set
我不知道为什么会出错,但如果您能提供帮助,我将非常感激。
这是我的 app.js代码和重要说明,我通过节点 package 管理器下载了 express 和 body-parser。
const express = require('express');
const bodyParser = require('body-parser');
const app = express;
app.set('view engine', 'ejs');
app.get("/",function(req,res){
var today = new Date();
var option = {
weekday:"long",
day:"numeric",
month:"long"
};
var day = today.toLocaleDateString("en-US",option);
res.sendfile(__dirname + "/index.html");
res.render('list', {kindOfDay:day});
});
app.listen(3000,function(){
console.log("Server started on port 3000");
});
我的错误
C:\Users\a\Desktop\Todo-List v1\app.js:6
app.set('view engine', 'ejs');
^
TypeError: app.set is not a function
at Object.<anonymous> (C:\Users\a\Desktop\Todo-List v1\app.js:6:5)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
尝试这个
const app = express();
Loading express module returns a function. So, Please try changing your code like this. const app = express();
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
app.get("/",function(req,res){
var today = new Date();
var option = {
weekday:"long",
day:"numeric",
month:"long"
};
var day = today.toLocaleDateString("en-US",option);
res.sendfile(__dirname + "/index.html");
res.render('list', {kindOfDay:day});
});
app.listen(3000,function(){
console.log("Server started on port 3000");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.