Wednesday, 3 May 2017

Cannot POST /api/register in Node Js

I am new at here and I am getting Can not post /api/register error in postman and 404 not found in console, I have checked all the possible question answers. Please assist me as soon as possible. Thanks in advance.

The auth.controller.js file

var express = require ('express'); //EXPRESS Package
var route = express.Router();   //define our app using express
// var userModel = require('../model/user.model');
// var session = require('express-session');
var bodyParser = require('body-parser');// get body-parser
route.use(bodyParser.json()); // for parsing application/json
route.use(bodyParser.urlencoded({ extended: true })); // for parsing   application/x-www-form-urlencoded
/**
 * 
 * Register user
 */
route.post('/api/register', function (req, res) {
    console.log(req.body);//res.sendStatus(200);
    // userModel.create(req.body).then( function () {

    //         res.sendStatus(200);
    // }).catch( function (err) {

    //     res.status(400).send(err);
    // });
});

/**
 * 
 * Login user
 */
route.post('/api/login', function (req, res) {

    // userModel.authenticate(req.body.email, req.body.password).then( function () {

    //     res.sendStatus(200);
    // }).catch( function (err) {

    //     res.status(400).send(err);
    // });
});
module.exports = route;

========================================================================== The server.js File

var express = require ('express'); //EXPRESS Package
var app = express();    //define our app using express

var cors = require('cors');
var session = require('express-session');
var bodyParser = require('body-parser');// get body-parser
// var morgan = require('morgan'); //use to see requests
// var assert = require('assert');
// var path = require('path');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(cors());

/**
 * Header Control
 */
app.use(function(req, res, next) {

  res.header('Access-Control-Allow-Credentials', true);
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH");
  res.header('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
  next();
} );
//routes
app.use('/api/register', require('./controllers/auth.controller'));
app.use('/api/login', require('./controllers/auth.controller'));
// app.use('/api/users', require('./controllers/users.controller'));

// app.get('/api', function (req, res) {
//     res.send('/api/login');
// } );
// start server
var server = app.listen(8001, 'localhost', function () {

    console.log('Server listening at http://' + server.address().address + ':' + server.address().port);
});

// module.exports = app;


via Sanjay Samant

No comments:

Post a Comment