Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New route in Express JS 404 error

Trying to add a new route to this Express app using the Express app generator. this is the app.js file:

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 parse = require('./routes/parse');

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('/', routes);
app.use('/users', users);
app.use('/parse', parse);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found - from app.js');
  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 lines i added are:

 var parse = require('./routes/parse');
   app.use('/parse', parse);

Then in the /routes directory i added parse.js that contains:

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


router.get('/parse', function(req, res, next) {
  res.send('return from parse.js');
});

module.exports = router;

I can hit the / and the /users resource using a browser and i get the expected result. But when i try to hit /parse i get this 404:

Not Found - from app.js

404

Error: Not Found - from app.js
    at /home/bitnami/parsecsvapi/app.js:32:13
    at Layer.handle [as handle_request] (/home/bitnami/parsecsvapi/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:312:13)
    at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:330:12)
    at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:271:10)
    at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:618:15
    at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:256:14)
    at Function.handle (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:176:3)
    at router (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:46:12)
like image 364
Sam Ucich Avatar asked Mar 25 '16 17:03

Sam Ucich


1 Answers

Try /parse/parse in the URL

If you would like to use /parse, please use / instead of /parse in /routes/parse.js

Please refer /routes/index.js & /routes/users.js.

TL;DR;

app.js

var parse = require('./routes/parse');
app.use('/parse', parse);

routes/parse.js

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

// the URL pattern will be used in conjunction with how its is being used
// in this case it would be '/parse' (from app.js) + '/parse' (as used in the next line)
// resulting in '/parse/parse' 
router.get('/parse', function(req, res, next) {
  res.send('return from parse.js');
});

module.exports = router;
like image 76
Sarbbottam Avatar answered Nov 12 '22 18:11

Sarbbottam