node.js - 运行 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);
})
解决方案
你需要更换
app.use(express.urlencoded())
和
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
并且不要忘记要求 bodyparser
var bodyParser = require('body-parser');
推荐阅读
- java - java - 为异步任务时间限制制作if
- c++ - 不能使用字符串类来分配字符,但可以在 C++ 中使用 char 数组
- javascript - 在 TypeScript 中声明类型“数字”
- php - 在woocommerce中买一送一,无需优惠券代码
- docker - docker checkout 或还原 docker diff 中的文件
- angular - 如何在 Ionic Creator 上使用 Injector 功能?我可以创建登录应用程序吗?
- mysql - 如何在存储过程中加入表
- c# - 托管在 Windows 服务中的 WCF 服务在停止时挂起
- css - CSS过滤器反转规则破坏Chrome 68上的固定位置
- spring-security - Postman 应用程序的基本 HTTP 身份验证