Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么我的NodeJS应用程序不能正常工作?_Node.js_Express - Fatal编程技术网

Node.js 为什么我的NodeJS应用程序不能正常工作?

Node.js 为什么我的NodeJS应用程序不能正常工作?,node.js,express,Node.js,Express,我不明白我的错误是什么,但是app.Method没有真正起作用 server.js : //express : var express = require('express') var app = express() var bodyParser = require('body-parser') //middleware : app.use(express.json); app.use(bodyParser.urlencoded({ extended:false })) //templat

我不明白我的错误是什么,但是
app.Method
没有真正起作用

server.js :

//express :
var express = require('express')
var app = express()
var bodyParser = require('body-parser')

//middleware :
app.use(express.json);
app.use(bodyParser.urlencoded({ extended:false }))

//templating engine :
var ejs = require('ejs');
app.set('view engine', ejs);

//public directory :
app.use(express.static(__dirname + '/public'));

//pages:
app.get('/', function (req, res) {
    console.log("Hello")
    res.render('pages/index')
})

app.get('/post/new', function (req, res) {
    res.render('pages/post-new')
})
app.post('post/new', function (err, req) {
    console.log(req.body)
})

//listening 
app.listen(8080, function (err, res) {
    if (err) throw err;
    else console.log(`Listening on 8080`)
}); 
我的目录是这样的:

node-modules
public
views
--pages
----index.ejs
----post-new.ejs
--partials
package-lock.json
package.json
server.js
我真的不明白为什么它不起作用。我曾尝试在
app.get
s中添加
console.log(req)
,但它们并没有真正记录任何内容。很抱歉问了这个愚蠢的问题


编辑:答案是否是
app.use(express.json())
而不是
app.use(express.json)
app.use(express.json)
->
app.use(express.json())
应该开始工作了。发生的情况是,您的中间件没有对请求调用
next()
,因为您传入了错误的函数,因此,没有一个请求会流到其余的方法。无论如何,我怎么会错过它呢?LOL New ERROR您的
app
bodyParser
也隐式声明为全局变量,而不是模块作用域。我建议这些行以
var
开头,尽管看起来您想用
而不是
来分隔它们。你说得对,我改了。再次感谢您的帮助。:)