【问题标题】:I get a TypeError: express.json() is not a function我得到一个 TypeError: express.json() is not a function
【发布时间】:2021-10-08 03:41:29
【问题描述】:

我是初学者,我一直在使用 github-learning-lab。 它声明使用 app.use(bodyParse.json()) 执行 POST 请求,但 VSCode 声明不推荐使用“body-parse”。

我在网上搜索,大多数人都说使用app.use(express.json()) 来获取当前版本的 Express

我的快递版本:4.17.1

我的节点版本:14.17.0

const express = require('express');
const app = express();

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

const mockUserData = [
    {name: 'Mark'},
    {name: 'Jill'}
]
app.get('/users', function(req, res){
    res.json({
        success: true,
        message: 'successfully got users. Nice!',
        users: mockUserData
    })
})
// colons are used as variables that can be viewed in the params
app.get('/users/:id', function(req,res){
    console.log(req.params.id);
    res.json({
        success: true,
        message: 'got one user',
        user: req.params.id
    })
})

app.post('/login', function(req,res){
    // Typically passwords are encrypted using something like bcrypt before sending to database
    const username = req.body.username;
    const password = req.body.password;

    // This should come from the database
    const mockUsername = 'billyTheKid';
    const mockPassword = 'superSecret';

    if (username === mockUsername && password === mockPassword) {
        // In practice, use JSON web token sign method here to make an encrypted token
        res.json({
            success: true,
            message: 'password and username match!',
            token: 'encrypted token goes here'
        })
    } else {
        res.json({
            success: false,
            message: 'password and username do not match'
        })
    }
})

app.listen(8000, function(){
    console.log('server is running')
})
Macs-MBP:node-express-course mac$ node server.js
/Users/mac/Documents/node-express-course/server.js:4
app.use(express.json());
                ^

TypeError: express.json is not a function
    at Object.<anonymous> (/Users/mac/Documents/node-express-course/server.js:4:17)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Macs-MBP:node-express-course mac$ head node_modules/express/package.json
{
  "_from": "express",
  "_id": "express@4.17.1",
  "_inBundle": false,
  "_integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
  "_location": "/express",
  "_phantomChildren": {},
  "_requested": {
    "type": "tag",
    "registry": true,

【问题讨论】:

  • 您确定拥有您认为拥有的 Express 版本吗?试试cat node_modules/express/package.json | jq .version(或者只是打开文件并检查版本)。 JSON 中间件仅适用于 v4.16.0
  • 我检查了 package.json 它的 v4.17.1
  • 那很奇怪。我根本无法重现这个问题。你确定所有的拼写都正确吗?确保将您的确切代码复制/粘贴到上述问题中。还请完整复制/粘贴确切的错误消息
  • 我刚刚更新了。
  • 不得已,试试旧的rm -r package-lock.json node_modules &amp;&amp; npm install

标签: node.js express body-parser


【解决方案1】:

我重新安装了快递。我最初安装它时可能犯了一个错误。我应该从一开始就尝试这个。感谢您的帮助。

【讨论】:

    【解决方案2】:

    首先你可以使用 Bodyparser 或者你应该重新安装它,但我推荐使用 Bodyparser

    【讨论】:

    • 我最初是这样做的,但它说 bodyparser 已被弃用。显然它仅适用于 4.16.1 之前的版本。我会尝试重新安装
    • BodyParser 已被弃用,因为它已被合并到 Express 本身中
    猜你喜欢
    • 2020-11-16
    • 2020-01-20
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多