404 Error - Express cannot find route

7,909 views
Skip to first unread message

Donald Hook

unread,
Sep 12, 2015, 1:46:11 PM9/12/15
to Express
I am trying to setup a new project and trying to define a simple route to verify the environment.  Express is not finding my route and I am not sure what I am doing wrong.

I am doing a GET to http://localhost:3000/salesforce  and get a 404.  The code is below.  When I do the GET, the [console.log('Something is happening.');] does write to the log, so I am not sure why it is not finding the route.

I am using Epxress 4.10.8

Thanks in advance ....
  

// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router

// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/salesforce', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});

app.use(function(req, res, next) {
console.log('** HTTP Error - 404 for request: ' + req.url);
res.status(404).send('Sorry cant find that!');
});
// more routes for our API will happen here

// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/salesforce', router);

Paul Vencill

unread,
Sep 12, 2015, 4:00:16 PM9/12/15
to expre...@googlegroups.com

The configuration you have here should respond to /salesforce/salesforce because you setup the mountpoint the way you did.

--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

Jishnu Viswanath

unread,
Sep 14, 2015, 6:16:07 AM9/14/15
to expre...@googlegroups.com
app.use('/', router);
Adding to what Paul mentioned, if you want api to start at root use above code.

Jishnu Viswanath
Software Engineer
Scienaptic Systems
Reply all
Reply to author
Forward
0 new messages