gpt4 book ai didi

javascript - 将参数传递给函数导出 Node

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:54 25 4
gpt4 key购买 nike

routes.js

var categories = require('../controllers/categories.server.controller');

module.exports = function(app) {

app.get('/',function(request,response){
return response.send("Welcome");
});

app.route('/')
.get(function(request, response) {
return response.send('Rayees');
});
.post(categories.create);

app.route('/:categoryId')
.get(categories.read)
.put(categories.update)
.delete(categories.delete)

app.param('categoryId', categories.categoryId)

};

app.js 文件

'use strict'

var express = require('express');

var path = require('path');

var favicon = require('serve-favicon');

var logger = require('morgan');

var cookieParser = require('cookie-parser');

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

var app = express(),

mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/pressworld');

var category = require('./server/routes/category.server.routes')(app);

app.use(logger('dev'));

app.use(bodyParser.json());

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

app.use(cookieParser());

app.use('/api/category', category);

我收到此错误

TypeError: Router.use() requires middleware function but got a undefined
at Function.use (D:\Development\reduxpress\node_modules\express\lib\router\index.js:458:13)
at EventEmitter.<anonymous> (D:\Development\reduxpress\node_modules\express\lib\application.js:219:21)
at Array.forEach (native)
at EventEmitter.use (D:\Development\reduxpress\node_modules\express\lib\application.js:216:7)
at Object.<anonymous> (D:\Development\reduxpress\app.js:49:5)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (D:\Development\reduxpress\bin\www:7:11)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)

最佳答案

利用express.Router并返回router obj并在express中间件中使用它。

请修改您的routes.js并使用router.get、router.post而不是app。

module.exports = function(app) {

var express = require('express');
var router = express.Router();


router.get('/',function(request,response){
return response.send("Welcome");
});

return router;

};

关于javascript - 将参数传递给函数导出 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36647144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com