Reputation: 15369
While walking through this tutorial, I came across a problem wherein the router methods would not work. Using npm start
and accessing localhost:3000/api/puppies gets a 404 error. However when I changed
var router = express.Router();
router.get('/api/puppies', db.getAllPuppies);
to
var app = express();
app.get('/api/puppies', db.getAllPuppies);
and run with node index.js
, the data prints as expected. I've tried also putting at the beginning of my file
app.use(express.static(__dirname + '/api/'));
but no joy. Is this something to do with npm start
? At one point I literally copy/pasted the code out of the tutorial and still I get the 404s.
Upvotes: 0
Views: 144
Reputation: 707876
A router has to be connected to your express app in order to be part of your server.
app.use(yourRouter);
Or, more commonly with a path that isolates that router's effect to just URLs that start with a specific path and the router's own URLs are relative to this path:
app.use('/somePath', yourRouter);
Without this, it's just a declared and configured router that isn't attached to any server.
Express documentation examples here.
The tutorial you reference does not appear to show this part of using a router.
Upvotes: 1