首页 > 解决方案 > 运行 nodejs 文件时出现警告消息

问题描述

我是节点js的初学者,我编写了一个代码来使用express从html获取表单数据,它总是显示警告。“body-parser 已弃用未定义扩展:提供扩展选项 app.js:11:17”

这是我的代码

const express = require('express')
const path = require('path')

const app=express()

const port=process.env.PORT || 3000

const publicDirectoryPath = path.join(__dirname, '')

app.use(express.static(publicDirectoryPath))
app.use(express.urlencoded())
app.use(express.json())


app.get('',(req,res)=>{
    res.render('index')
}) 

app.post('/login',(req,res)=>{
    try{
        console.log(req.body)
        res.send('thankyou for submission')
    }catch(error){
        res.send()
    }
})

app.listen(port,()=>{
    console.log('server started to'+port);
  })

标签: node.jsexpress

解决方案


你需要更换

app.use(express.urlencoded())

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

并且不要忘记要求 bodyparser

var bodyParser  = require('body-parser');

推荐阅读