Express TypeError:bodyParser.json不是nuxt.js中的函数
我收到一条错误消息,指出:TypeError:bodyParser.json不是函数。我的nuxt.config.js文件包含以下有关bodyparser的详细信息(我最初使用const bodyparser=require('body-parser'),但出现了一个错误,告诉我必须使用'import'而不是'require',因此我将其更改为'import('body-parser'): 在api文件夹下的index.js文件中,我有以下代码:Express TypeError:bodyParser.json不是nuxt.js中的函数,express,nuxt.js,body-parser,nuxtjs,Express,Nuxt.js,Body Parser,Nuxtjs,我收到一条错误消息,指出:TypeError:bodyParser.json不是函数。我的nuxt.config.js文件包含以下有关bodyparser的详细信息(我最初使用const bodyparser=require('body-parser'),但出现了一个错误,告诉我必须使用'import'而不是'require',因此我将其更改为'import('body-parser'): 在api文件夹下的index.js文件中,我有以下代码: const express = require(
const express = require('express')
const bodyParser = require('body-parser')
const router = express.Router()
const app = express()
router.use((req, res, next) => {
Object.setPrototypeOf(req, app.request)
Object.setPrototypeOf(res, app.response)
req.res = res
res.req = req
next()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
})
router.post('/track-data', (req, res) => {
console.log('Stored data!', req.body.data)
res.status(200).json({ message: 'Success!' })
})
module.exports = {
path: '/api',
handler: router
}
有人知道如何运行它吗?每次我在终端中输入'npm run dev',我都会得到错误'TypeError:bodyParser.json不是一个函数。我不太熟悉
nuxtjs
,但在查看后,它看起来像是索引中的require
,js
应该是文件的路径,老鼠她不仅仅是字符串body解析器
,我必须想象,如果没有提供路径,它会像npm模块一样被处理
此外,至少在NodeJS和per and中,导入使用export default
导出的内容的语法是import{destructuredModuleName}from'string/representing/relative/or/absolute/path/to/module'
或
import*作为您想从'string/representation/relative/or/absolute/path/to/module'调用的对象,
将为您提供一个更传统的模块对象,其属性/方法与导出模块上的属性相匹配。我不太熟悉numxtjs
具体来说,但在查看之后,它看起来像您的需求在index.js中的e
应该是文件的路径,而不仅仅是字符串body解析器
,我必须想象,如果没有提供路径,它会像npm模块一样被处理
此外,至少在NodeJS和per and中,导入使用export default
导出的内容的语法是import{destructuredModuleName}from'string/representing/relative/or/absolute/path/to/module'
或
import*正如您想从'string/representation/relative/or/absolute/path/to/module'调用对象一样,
将为您提供一个更传统的模块对象,其属性/方法与导出模块上的属性相匹配。我遇到了一个稍微类似的问题,可以通过此代码解决它,首先我在配置文件中导入了express,在我像您一样使用bodyParser之前,但得到了一个不推荐使用的警告,然后我在api文件夹中的索引文件中使用它,如下所示:
// nuxt.config.js
import express from 'express';
export default {
ssr: true,
.......,
.......,
serverMiddleware: [
express.json(),
// Api middleware
{ path: '/api', handler: '~/api/index.js' },
]
}
// ~/api/index.js
// Router setup for serverMiddleware
router.use((req, res, next) => {
Object.setPrototypeOf(req, app.request);
Object.setPrototypeOf(res, app.response);
req.res = res;
res.req = req;
next();
});
export default {
path: '/api',
handler: router
};
希望有帮助!我遇到了一个稍微类似的问题,可以用这段代码解决它,首先我在配置文件中导入了express,然后像您一样使用bodyParser,但得到了一个不推荐的警告,然后我在api文件夹中的索引文件中使用它,如下所示:
// nuxt.config.js
import express from 'express';
export default {
ssr: true,
.......,
.......,
serverMiddleware: [
express.json(),
// Api middleware
{ path: '/api', handler: '~/api/index.js' },
]
}
// ~/api/index.js
// Router setup for serverMiddleware
router.use((req, res, next) => {
Object.setPrototypeOf(req, app.request);
Object.setPrototypeOf(res, app.response);
req.res = res;
res.req = req;
next();
});
export default {
path: '/api',
handler: router
};
希望有帮助!