Sarbjyot Singh Chahal Sarbjyot Singh Chahal - 3 years ago 138
Node.js Question

NodeJs routes are no working

I am trying to create an api for login in nodejs.but when i check the login route via Http requester it showingCannot "POST /api/authenticate"

The project structure:

enter image description here

server.js file:



var express = require('express');
var app = express();
var port = process.env.PORT||8080;
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var router = express.Router();
var appRoutes = require('./app/routes/api')(router);
var path = require('path');

app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.use('/app',appRoutes);


mongoose.connect('mongodb://localhost:27017/tutorial', function(err){
if(err) {
console.log('Not connected to the Database: '+ err);
}
else {
console.log('Succesfully connected to Mongodb');
}
});

app.get('*',function(req,res){
res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
});

app.listen(port, function () {
console.log('Running the server on port ' + port);
});





/app/models/user.js:



var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

var UserSchema = new Schema({
username: { type: String, lowercase: true, required: true, unique: true },
password: { type: String, required: true },
email: { type: String, lowercase: true, required: true, unique: true }

});

UserSchema.pre('save', function(next) {
var user = this;
bcrypt.hash(user.password, null, null, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
next();
});


module.exports = mongoose.model('User', UserSchema);





/app/routes/api.js:



var User =require('../models/user');

module.exports = function(router) {
//http://localhost:8080/api/users
//User Registration Route
router.post('/users', function(req,res){
var user = new User();
user.username = req.body.username;
user.password = req.body.password;
user.email = req.body.email;
if (req.body.username == null || req.body.username == '' || req.body.email == null || req.body.email == ''|| req.body.password == null || req.body.password == '' ){
res.send('Ensure Username, Email and Password is provided');

} else {
user.save(function(err){
if(err){
res.send('Username or Email already exist');
}
else {
res.send('User created!!!')
}
});
}

});
//User login Route
//http://localhost:8080/api/authenticate
router.post('/authenticate',function(req,res) {
res.send('Testing the route');
});

return router;
}





and finally HTTP requester output (it is working fine for register api in /app/models/api.js)

enter image description here

Answer Source

The reason seems to be this:

app.use('/app',appRoutes);

This adds your routes with a prefix of /app, which should be /api:

app.use('/api', appRoutes);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download