gpt4 book ai didi

node.js - EmberJS Express API | TypeError : app. 路由不是 module.exports 中的函数

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

我的 ember 项目的服务器 API 困扰了我一周了。我无法让模块化 API 工作,这让我发疯。

控制台相关:
mongod、 Node 服务器、heroku local(或 ember s)

首先,我试图解决的错误:(在转到 http://localhost:4500/notes 时显示)

TypeError: app.route is not a function
at module.exports (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\routes\noteRoute.js:21:9)
at Layer.handle [as handle_request] (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:275:10)
at jsonParser (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\body-parser\lib\types\json.js:109:7)
at Layer.handle [as handle_request] (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\body-parser\lib\types\urlencoded.js:91:7)
at Layer.handle [as handle_request] (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Accurate\Projects\emberjs\accurate2\AccurateEmber2\api\node_modules\express\lib\router\index.js:284:7

这是我的文件夹架构:
API
- - Controller
- - - - noteController.js
- -模型
- - - - noteModel.js
- -node_modules
- -路线
- - - - noteRoute.js
--package-lock.json
--package.json
--server.js
应用
- - 序列化器
- - - - note.js
配置
距离
node_modules
公开
服务器
ETC...

<小时/>我的 server.js 文件:

// Allow POST, GET, PUT, DELETE, OPTIONS
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
});

//DB Vars
var uri = 'mongodb://localhost/Ember2';

// mongoose instance connection url connection
mongoose.Promise = global.Promise;

var options = {
useMongoClient: true
};
mongoose.connect(uri, function (err) {
if (err) {
console.log('ERROR connecting to: ' + uri + '. ' + err);
} else {
console.log('Succeeded connected to: ' + uri);
}
});

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


var note = require('./routes/noteRoute'); //importing route
app.use('/notes', note);

//middleware catch
app.use(function (req, res) {
res.status(404).send({ url: req.originalUrl + ' not found' })
});

app.listen(port);


console.log('RESTful API server started on: ' + port);

我的 noteRoute.js:(注释代码是我尝试过的替代方案,但仍然收到错误。本质上,我无法与我的数据库交互。)

'use strict';
/*let router = require('express').Router();
let note = require('../controllers/noteController');

router.post('api/notes', note.create_a_note);
router.get('api/notes', function (req, res){
note.list_all_notes});

module.exports = router;*/

module.exports = function (app) {
let note = require('../controllers/noteController');
let router = require('express').Router();

router.post('api/notes', note.create_a_note);
router.get('api/notes', function (req, res) {
note.list_all_notes
});

// note Routes
/*app.route('/notes')
.get(note.list_all_note)
.post(note.create_a_note);

app.route('/notes/:noteId')
.get(note.read_a_note)
.put(note.update_a_note)
.delete(note.delete_a_note);*/
};

我的noteModel.js:

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

let NoteSchema = new Schema({
title: {
type: String,
required: 'Kindly enter the name of the note'
},
content: {
type: String
},
author: {
type: String
}
});

module.exports = mongoose.model('Notes', NoteSchema);

我的noteController.js:

'use strict';

let mongoose = require('mongoose'),
Note = mongoose.model('Notes');

exports.list_all_notes = function(req, res){
Note.find({}, function(err, note){
if (err)
res.send(err);
res.json(note);
});
};

exports.create_a_note = function(req, res){
let new_note = new Note(req.body);
new_note.save(function (err, note){
if (err)
res.send(err);
res.json(note);
});
};

exports.read_a_note = function(req, res){
Note.findById(req.params.noteId, function(err, note){
if (err)
res.send(err);
res.json(note);
});
};

exports.delete_a_note = function(req, res){
Note.remove({
_id: req.params.noteId
}, function(err, task){
if (err)
res.send(err);
res.json({ message: 'Note successfully deleted.' });
});
};

我的 note.js 序列化器:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
/*primaryKey: '_id',
serializeId: function (id) {
return id.toString();
}*/
normalizeResponse(store, primaryModelClass, payload, id, requestType){
payload = {notes: payload};

payload.notes.foreach((note) => {
note.id = note._id;
delete note._id;
});

return this._super(store, primaryModelClass, payload, id, requestType);
}
});

我一直在使用 Postman 和浏览器的组合来检查 API 状态。
随着应用程序变得越来越大,必须拥有这种模块化的 API。将所有 get、post、put、delete 等都放在 server.js 中将会变得一团糟。

最佳答案

noteRoutes.js 应该是这样的:

let router = require('express').Router();
let note = require('../controllers/noteController');

router.post('/api/notes', note.create_a_note);
router.get('/api/notes', note.list_all_notes);

module.exports = router;

如果您随后使用此(您已有的)将其包含在 server.js 中:

var note = require('./routes/noteRoute');
app.use('/notes', note);

这将为您的 GETPOST 请求提供 http://localhost:4500/notes/api/notes 的 URL。如果这不是您想要的 URL,那么您需要相应地调整路由路径。例如如果您只想http://localhost:4500/notes,则将路径更改为router.get('/', note.list_all_notes);,对于post()调用也是如此。

然后您需要修复 Controller 的问题。您已多次使用此模式:

if (err)
res.send(err);
res.json(note);

这不起作用,在错误情况下,它将尝试调用 res.sendres.json,这将失败,因为您只能发送一个响应。

很可能还有其他问题,但是当您遇到相关错误时,您需要对其进行调试。上述建议应该足以帮助您克服当前的错误。

关于node.js - EmberJS Express API | TypeError : app. 路由不是 module.exports 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777382/

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