Was working on creating a REST API using Node.js and Express. The server starts at localhost:xxxx but localhost:xxxx/bits returns a 404 Not Found error.
var express = require('express');
var app = express();
app.get('/bits', function(req, res) {
console.log("Bits called");
});
app.get('/bits/:id', function(req, res) {
console.log("Bits ID called");
});
app.listen(xxxx);
console.log('Listening on port xxxx.');
Help?
I just ran your code but with replacing xxxx with a port number. It worked fine for me.
var express = require('express');
var app = express();
app.get('/bits', function(req, res) {
res.send([{name:'bit1'}, {name:'bit2'}]);
});
app.get('/bits/:id', function(req, res) {
res.send({id:req.params.id, name: "The Name", description: "description"});
});
app.listen(8080);
console.log('Listening on port 8080.');
I ran the some curl commands as well to show it working.
[09:24 AM] jsloyer@Jeffs-MacBook-Pro [Downloads]>curl http://127.0.0.1:8080/bits
[{"name":"bit1"},{"name":"bit2"}]
[09:25 AM] jsloyer@Jeffs-MacBook-Pro [Downloads]>curl http://127.0.0.1:8080/bits/1
{"id":"1","name":"The Name","description":"description"}
[09:25 AM] jsloyer@Jeffs-MacBook-Pro [Downloads]>
Additionally here is a whole Node.js git project including the source for it.
You can click the button below to deploy it as well to see it in action.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With