I have been stumped by a server api for my ember project for a week now. I cannot get a modular api to work and it's driving me nuts.
Console related:
mongod, node server, heroku local(or ember s)
First, the error I'm trying to resolve: (shows when going to 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
Here is my folder schema:
api
- -controllers
- - - - noteController.js
- -models
- - - - noteModel.js
- -node_modules
- -routes
- - - - noteRoute.js
- -package-lock.json
- -package.json
- -server.js
app
- - serializers
- - - - note.js
config
dist
node_modules
public
server
etc...
My server.js file:
// 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);
My noteRoute.js: (commented code are alternatives I have tried and still received the error. Essentially, I am unable to interact with my database.)
'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);*/
};
My 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);
My 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.' });
});
};
My note.js serializer:
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);
}
});
I have been using a combination of Postman and the browser to check on API status.
Having this modularized API is a must as the application will grow much larger. Having all of the get, post, put, delete, etc. in the server.js will be a mess.
Aucun commentaire:
Enregistrer un commentaire