Question/Problem
My req.body is undefined when I post form data... This is strange because I have multiple other pages with forms where I post data the same way but all of a sudden I am getting this issue and I do not know why. This does seem to be a common problem but I can't find a fix that works for me.
What I have tried so far...
Checked console.log(req.is('json')) but it is false and I do not know what to do with this information - I feel like this might be the source of my problem. It doesn't look like I can set form data to json
I have other forms which I coded in a similar manner that work
Included 'body-parser'/app.use(bodyParser.json());... see the above point
Tried using app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
Made sure I have a name property for each input field
EDIT: app.use(bodyParser.urlencoded()); does not work
Code
<form action="/changePassword" method="post" onsubmit="event.stopPropagation(); return passwordChange(this);" enctype="application/x-www-form-urlencoded" novalidate="">
<input id="password1" placeholder="password" name="password1" maxlength="30" class="form-control input-sm chat-input" type="password">
<input id="password2" placeholder="confirm password" name="password2" maxlength="30" class="form-control input-sm chat-input" type="password">
<input id="changePassword" value="Change Password" type="submit">
</form>
app.post('/changePassword', users.changePassword);
module.exports.changePassword = function(req, res){
console.log(req.is('json')); // false and req.body is undefined
...
...
res.redirect('/');
});
Let me know if additional code is needed.
EDIT: Not sure if this is worth mentioning but I had other problems which again I did not have with other pages. When I was trying to validate the passwords, return false; would not prevent the page from posting/executing. I had to include event.stopPropagation().
Additional Code:
./bin/www
!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('app:server');
var http = require('http');
var express= require('express');
/**
* Get port from environment and store in Express.
*/
var port = parseInt(process.env.PORT, 10) || 3000;
app.set('port', port);
/**
* Create HTTP server.
*/
// var server = http.createServer(app);
//var server = app;
/**
* Listen on provided port, on all network interfaces.
*/
app.use((err, req, res, next) => {
console.log(err.stack || err.message);
if (res.headersSent)
return next(err)
res.status(500).send('Internal Server Error')
})
app.listen(port);
app.on('error', onError);
app.on('listening', onListening);
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error('Port ' + port + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error('Port ' + port + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
debug('Listening on port ' + server.address().port);
}
app.js
/*jslint node:true */
/*global $, jQuery, alert*/
"use strict";
var express = require('express');
var path = require('path');
var fs = require('fs');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressSession = require('express-session');
var mongoose = require('mongoose');
var mongoStore = require('connect-mongo/es5')({ session: expressSession });
var passport = require('passport');
var debug = require('debug')('app:server');
var uriUtil = require('mongodb-uri');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var assert = require('assert');
mongoose.Promise = global.Promise;
var MONGOLAB_URI = ****;
var mongooseUri = uriUtil.formatMongoose(MONGOLAB_URI);
var mongoConnection = mongoose.connect(MONGOLAB_URI, function (err, db) {
if (err) {
console.log('Unable to connect to MongoDB: ', err);
} else {
console.log('Connection has been established...');
}
});
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
console.log("__dirname: " + __dirname);
app.use(expressSession({
secret: 'RCNEfyBUAcHnPeQxFWyBTr',
cookie: { maxAge: 60*60*1000 },
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
require('./routes/index')(app, conn);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
exports.mongoConnection = mongoConnection;
index.js
module.exports = function (app, conn) {
...
app.post('/signup', users.signup);
app.post('/login', users.login)
app.post('/changePassword', users.changePassword);
app.post('/password', users.password); // send new password
app.post('/user/create_test', users.create_test);
app.post('/user/calculatesubjects', users.calculate_subjects);
app.post('/user/calculatetopics', users.calculate_topics);
app.post('/test/:username/:testid/:noq/:quesnum', users.answer_selected);
// Admin posts
app.post('/admin/update_subjects_topics', admin.update_subjects_topics);
app.post('/admin/add_question', admin.add_question);
}
passwordChange function
function passwordChange(form)
{
if(form.password1.value != "" && form.password1.value == form.password2.value) {
if(form.password1.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form.password1.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form.password1.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.password2.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form.password2.focus();
return false;
}
return true;
}
It does sound like the problem is that the form is not being sent in JSON. By default html forms are sent as urlencoded (which you specified). Unless you are sending the form via javascript and specify json you should be using bodyParser.urlencoded:
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
I am confused to how other forms made similarly are working. The forms still can be sent via json if you use something like jQuery to send them via a script (and not with native html).
Related
This photo shows --ms-- message. And in client side, I get timeout error.
I am trying to figure out where it occurs and why using try-catch and console.log but I can't find any.
This is 'routes/email.js'
var express = require('express');
var router = express.Router();
var db = require('../db');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'ssssss#gmail.com',
pass: 'SSSSSS'
}
});
router.get('/send', function(req, res, next) {
console.log('/send');
var email = decodeURI(req.query.to);
console.log(email);
var rand = Math.floor((Math.random() * 9000 + 1000));
var link="http://"+req.get("host")+"/email/verify?email="+email+"&num="+rand;
var mailOptions={
to : email,
subject : "Please confirm your Email account",
html : "Hello,<br> Please Click on the link to verify your email.<br>Click here to verify"
}
try{
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.json({result:false,message:"Incorrect email", code:404});
}else{
console.log("Message sent:\n"+JSON.stringify(response, null, 2));
try{
console.log(JSON.stringify(db, null, 2));
var sql = "INSERT INTO email_verification(email, code) VALUES(?,?)";
var input = [email, rand];
db.get().query(sql, input, function(err, result){
if(err) res.json({result:false, message:"SQL error", code:403});
res.json({result:true, message:"Please check your email.", code:100});
});
// res.json({result:false, message:"DB ERROR", code:401});
}catch(e){
console.log(e);
}
}
});
}catch(e){
console.log(e);
}
});
module.exports = router;
db.get().query() part doesn't execute at all.
This is db.js.
const mariadb = require('mariadb');
var pool;
exports.connect = function(done){
console.log("Trying to connect DB...");
pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: 'ssss',
database:"SSSS",
connectionLimit: 100
});
}
exports.get = function(){
console.log("exports.get");
return pool;
}
The console.log("exports.get"); parts works fine.
This is app.js:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var db = require('./db');
//var passport = require('./passport');
//var auth = require('./routes/auth');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
db.connect(function(err){
console.log(err);
if(err){
console.log('Unable to connect to MariaDB.');
process.exit(1);
}
});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', require("./routes/index"));
app.use('/email', require("./routes/email"));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(404);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
I just used this db.jsand app.js file from my other project and it worked fine there. But I don't know why this time I have this problem.
Which part and why I am having this problem and how can I fix it?
It'd be easier to work with node.js if I can track the error easily. Figuring out the problematic part takes lots of time. Is there any module or tool for that?
I'm using express.js, rethinkdb, thinky, socket.io. Trying to notify the index page of any changes that happens in the db. So I use rethinkdb changesfeed (and I can see the updates in console.log) but the cursor/feed is showing object object instead of the data.
When I pass the feed results to the socket.emit, it displays [object object]?
I read and implemented the code that comes from the rethinkdb blog (https://www.rethinkdb.com/blog/cats-of-instagram/) but it is not working.
Please scroll down to the bottom with socket.io header
App.js
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 r = require('rethinkdb');
// *********** Thinky code*********************//
var changeStream = require("rethinkdb-change-stream");
var thinky = require('thinky')()
var type = thinky.type;
var r = thinky.r;
//thinky model
var User = thinky.createModel('User', {
author: type.string()
});
/*
User.changes().then(function (feed) {
feed.each(function (error, doc) {
if (error) {
console.log(error);
process.exit(1);
}
if (doc.isSaved() === false) {
console.log("The following document was deleted:");
console.log(JSON.stringify(doc));
}
else if (doc.getOldValue() == null) {
console.log("A new document was inserted:");
console.log(JSON.stringify(doc));
}
else {
console.log("A document was updated.");
console.log("Old value:");
console.log(JSON.stringify(doc.getOldValue()));
console.log("New value:");
console.log(JSON.stringify(doc));
}
});
}).error(function (error) {
console.log(error);
process.exit(1);
});
*/
//*********End Thinky Code ******************//
// Mongodb Example http://www.guru99.com/node-js-mongodb.html
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost/EmployeeDB';
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
//attach socket server to express server so it interacts with clients requests and responses
app.io = require('socket.io')();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/* )))))))))))))))) socket.io Header ((((((((((((((((((( */
app.io.on('connection', function (socket) {
r.connect(host = 'localhost', port = 28015, db = 'test').then(function (conn) {
this.conn = conn;
return r.table("User").changes().run(this.conn).then(function (cursor) {
cursor.each(function (err, item) {
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
});
})
});
// socket.emit("message-from-server", { greeting: "Say whatever here- it will show on the index page"});
});
module.exports = app;
From your code:
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
This tells me that item is probably an object so when you pass it to the web api to add to the DOM it renders the object reference [object Object] because its expecting a string (not an object). So you can pull out the info you want from item using . syntax for objects or [<index>] syntax for arrays, or you could do something like item.toString() to attempt to turn the whole thing into a string. It really depends on the structure of item and how you are adding it to the DOM. You could do it on the client side or do it before it's emitted from your server.
I am having problems trying to access the "DB" database object that is created when the MongoDB client module connects to my MongoDB database.
At the moment I am getting an error stating that, within data.js, 'db' is not defined. I understand why this is - the db object is not being "passed" through to the router and then subsequently through to the controller.
What is the best way to do this?
I have tried to pass the "db" object through to the router (dataRoutes.js) but I cannot figure how to make this accessible to the controller (data.js). Could someone please help?
Please note I have not included the other routes and controllers but they simply submit a Form via the POST method to /data/submit . The controller below is meant to write this form data to the MongoDB database.
Here is the relevant code:
app.js
var express = require('express');
var path = require('path')
var MongoClient = require('mongodb').MongoClient;
var bodyParser = require('body-parser');
var app = express();
var routes = require('./routes/index');
var dataRoutes = require('./routes/dataRoutes');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
MongoClient.connect("mongodb://localhost:27017/m101", function(err, db) {
if(err) throw err;
console.log("Successfully connected to MongoDB.");
app.use('/', routes); // Use normal routes for wesbite
app.use('/data', dataRoutes);
app.get('/favicon.ico', function(req, res) {
res.send(204);
});
app.use(function(req, res, next) {
var err = new Error('Oops Page/Resource Not Found!');
err.status = 404;
next(err); //Proceed to next middleware
});
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
// update the error responce, either with the error status
// or if that is falsey use error code 500
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
app.use(function(err, req, res, next) {
console.log('Error');
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
});
dataRoutes.js
// router
var express = require('express');
var router = express.Router();
// controller references
var ctrlsData = require('../controllers/data');
router.post('/submit', ctrlsData.submit);
module.exports = router;
data.js
var MongoClient = require('mongodb').MongoClient;
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
module.exports.submit = function(req, res) {
var title = req.body.title;
var year = req.body.year;
var imdb = req.body.imdb;
/*
console.log('submitted');
console.log(req.body);
sendJsonResponse(res, 201, {title,year,imdb});
*/
var title = req.body.title;
var year = req.body.year;
var imdb = req.body.imdb;
if ((title == '') || (year == '') || (imdb == '')) {
sendJsonResponse(res, 404, {
"message": "Title, Year and IMDB Reference are all required."
});
} else {
db.collection('movies').insertOne(
{ 'title': title, 'year': year, 'imdb': imdb },
function (err, r) {
if (err) {
sendJsonResponse(res, 400, err);
} else {
sendJsonResponse(res, 201, "Document inserted with _id: " + r.insertedId + {title,year,imdb});
}
}
);
}
};
Create a db variable that reference mongodb in app.js :
MongoClient.connect("mongodb://localhost:27017/m101", function(err, db) {
app.db = db;
//.....
});
In data.js, access db from req.app :
module.exports.submit = function(req, res) {
req.app.db.collection('movies').insertOne({ 'title': title, 'year': year, 'imdb': imdb },
function(err, r) {}
)
};
The accepted answer isn't quite correct. You shouldn't attach custom objects to the app object. That's what app.locals is for. Plus, the accepted answer will fail when using Typescript.
app.locals.db = db;
router.get('/foo', (req) => {
req.app.locals.db.insert('bar');
});
Sure, it's longer. But you get the assurance that future updates to ExpressJS will not interfere with your object.
I understand that the answer of #Bertrand is functional, but it is not usually recommended. The reason being that, from a software point of view, you should have a better separation in your software.
app.js
var express = require('express');
var path = require('path')
var MongoClient = require('mongodb').MongoClient;
var bodyParser = require('body-parser');
var app = express();
var routes = require('./routes/index');
var dataRoutes = require('./routes/dataRoutes');
var DB = require('./db.js');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
DB.Init("mongodb://localhost:27017/m101")
.then(() => {
console.log("Successfully connected to MongoDB.");
app.use('/', routes); // Use normal routes for wesbite
app.use('/data', dataRoutes);
app.get('/favicon.ico', function(req, res) {
res.send(204);
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
})
.catch((e) => {
console.log("Error initializing db");
});
db.js
var _db = null;
module.exports = {
Init: (url) => {
return new Promise((resolve, reject) => {
if (!url)
reject("You should provide a URL");
MongoClient.connect("mongodb://localhost:27017/m101", function(err, db) {
if(err) reject(err);
_db = db;
resolve(); // Or resolve(db) if you wanna return the db object
});
});
},
Submit: (req, res, next) => {
// Whatever goes. You have access to _db here, too!
}
};
in data.js
var DB = require('../db.js');
router.post('/submit', DB.submit);
Finally, even this answer can be improved as you are not usually advised to wait for the DB to connect, otherwise, you are losing the advantage of using ASync procs.
Consider something similar to here in app.js:
Promise.resolve()
.then(() => {
// Whatever DB stuff are
// DB.Init ?
})
.then(() => {
// Someone needs routing?
})
...
.catch((e) => {
console.error("Ther app failed to start");
console.error(e);
});
I understand that in the last sample, you can not instantly query DB as it may not have connected yet, but this is a server, and users are usually expected to wait for your DB to init. However, if you wanna more proof solution, consider implementing something yourself in DB.submit to wait for the connect. Or, you can also use something like mongoose.
I just started working with node.js and socket.io.
bin/www
var app = require('../app');
var debug = require('debug')('untitled:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
//var server = http.createServer(app);
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log('A new user connected'); <---- Not working
});
});
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
app.js
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 routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, '/node_modules/bootstrap/dist')));
app.use(express.static(path.join(__dirname, '/node_modules/jquery/dist')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
The node.js program will work the only thing is that I can not get the print out 'A new user connected' from my console. So it makes me think that there is some problems with my socket.io.
Did you at least include
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
in your client-side html? Otherwise, no connection is openend...
try to do 2 things:
put server.listen function above call to socket
change your call to io like this:
var io = require('socket.io').listen(server);
Noob - trying to set up CRUD using routes on Express4 and Node. I've got the following GET and POST routes working ok, but DELETE and PUT are giving 404, Not Found errors, which is strange.
I'm using Postman for Chrome and have set the Content Type to application/json, and this is working ok - i do notice that on the DELETE and PUT queries the header still notes that it's sending as text/html, rather than JSON, although I've set both the RAW settings to JSON, and the Headers content type manually to application/json.
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Todo = require('../models/Todo.js');
/* GET /todos listing. THIS IS WORKING*/
router.get('/', function(req, res, next) {
Todo.find(function (err, todos) {
if (err) return next(err);
res.json(todos);
});
});
/* POST /todos listings THIS IS WORKING*/
router.post('/', function(req, res, next){
Todo.create(req.body, function(err, post){
if (err) return next(err);
res.json(post);
});
});
/* GET /todos/id THIS IS WORKING*/
router.get('/:id', function(req, res, next) {
Todo.findById(req.params.id, function (err, post) {
if (err) return next(err);
res.json(post);
});
});
/* DELETE /todos/:id NOT WORKING*/
router.delete('/:id', function(req, res, next) {
console.log(req);
Todo.findByIdAndRemove(req.params.id, req.body, function (err, post) {
if (err) return next(err);
res.json(post);
});
});
module.exports = router;
Also this is a routes.js script that is being imported into app.js (see below):
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 routes = require('./routes/index');
var todos = require('./routes/todos');
// set our port
var port = process.env.PORT || 3000;
//Requires the mongoose connection
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/todoApp', function(err) {
if(err) {
console.log('connection error', err);
} else {
console.log('connection successful');
}
});
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/todos', todos);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// start app ===============================================
// startup our app at http://localhost:3080
app.listen(port);
// shoutout to the user
console.log('Magic happens on port ' + port);
module.exports = app;
I looked into your implementation, it looks fine. Just one chance where i feel it can go wrong is, if we look into the documentation of Model.findOneAndRemove(conditions, [options], [callback]), you are going wrong in [options].
In [options] we can do :
sort: if multiple docs are found by the conditions, sets the sort
order to choose which doc to update
select: sets the document fields to return
Try this, i hope it will solve your problem.
You mentioned that, PUT is also having same 404 issue, could you please update your post by including same. Let's understand where it is going wrong.
There's a couple of things you might want to check.
Your POST is working as intended, but your DELETEs and your PUTs aren't. You've set the content type to application/json.
Given that you are using Express 4, and had to include a body parser yourself, do you happen to include the following lines?
var app = express();
// you probably have this, for application/x-www-form-urlencoded
app.use(bodyParser.urlencoded());
// extrapolating here, you only mentioned you were receiving 404s.
// you might be missing the following line?
app.use(bodyParser.json());
In your delete route, you log the response object, is this a typo? A more useful log would be of the request object - so you can determine what exactly the route thinks you sent.
Shot in the dark, but in the absence of additional information, I hope at least one of the answers are correct.