Nick El Mir
Nick El Mir

Reputation: 59

Post routes error in Express

I'm facing a problem every time I try to create a post route in Express.

I get a Not found error.

Everything was working well before and I don't know what is really happening

This is my code:

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 index = 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', 'hbs');

// 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('/', index);
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 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;

And this is the users route:

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.post('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

Any help ?

Upvotes: 1

Views: 1242

Answers (1)

Sebastian S
Sebastian S

Reputation: 867

Your code looks fine. Try to:

  1. restart the app and make the post one again, check the logs. You may find an answer in the app logs . If still not working.
  2. Verify the index path ./routes/index
  3. If you still get not results . Reinstall the app again. Looks you are just starting so.

Upvotes: 1

Related Questions