Grammin Grammin - 2 months ago 8
reST (reStructuredText) Question

Why do my rest endpoints not work on my node server?

My index.js file looks like this:

var http = require('http');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser')

var app = express();
var currentVideo = 'https://www.youtube.com/embed/9Q-ovNuireY';

// parse application/json
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(res) {
console.log("sup");
});

app.get('/currentVideo', function(req, res) {
res.send(200, {youtubeSrc: currentVideo});
});

http.createServer(app).listen(process.env.PORT ||3000, function() {
console.log('Example app listening on port 3000!');
});


My static page in public loads up and my server starts up fine and prints out the Example app log. When I try to hit my get endpoint though it doesn't work. I've been hitting http://localhost:{port}/currentVideo and I get a 404.

Answer

I copied your code and tested it locally using curl. The request worked fine.

Could you share your public page? Any log is noticed in Chrome Developer Tools Console when you try to load data?

curl -v http://localhost:3000/currentVideo
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /currentVideo HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 58
< ETag: W/"3a-FMsS5hMXa1tcIFnGeYHinQ"
< Date: Sun, 18 Sep 2016 20:34:04 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
{"youtubeSrc":"https://www.youtube.com/embed/9Q-ovNuireY"}
Comments