EmberJS Express API | TypeError:app.route不是module.exports的函数

我已经被一个服务器api难住了一个星期,现在我的余烬项目。 我无法得到一个模块化的API工作,这让我疯狂。

控制台相关:
mongod,节点服务器,heroku本地(或者余烬)

首先,我试图解决的错误:(显示何时去http:// localhost:4500 /笔记 )

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
– 控制器
– – – – noteController.js
– -楷模
– – – – noteModel.js
– -node_modules
– – 路由
– – – – noteRoute.js
– -package-lock.json
– -package.json
– -server.js
应用
– 序列化程序
– – – – note.js
configuration
DIST
node_modules
上市
服务器
等等…


我的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 :(评论代码是我尝试过的替代scheme,仍然收到错误。基本上,我无法与我的数据库进行交互。

 '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); } }); 

我一直在使用邮递员和浏览器的组合来检查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请求的URL: http://localhost:4500/notes/api/notes 。 如果这不是您想要的URL,那么您需要相应地调整pathpath。 例如,如果您只想要http://localhost:4500/notes则将path更改为router.get('/', note.list_all_notes);post()调用相同。

然后你需要解决你的控制器的问题。 你已经多次使用这种模式:

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

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

可能还有其他问题,但是当你遇到相关的错误时,你需要debugging它们。 上面的build议应该足以让你通过你当前的错误。