javascriptnode.jshttphttpmodule

Node.js HTTP Server- Taking long time to load and is not sending data


I am learning node.js, While using HTTP module i tried to create by own server as per instruction of video tutor but my server is not sending any response at port 3000 and port 8000.

*

const http = require('http');
const server = http.createServer((req, res) => { 
    if(req === '/'){ 
        res.write('Hello world, first program in server using http module');
        res.end();
    }
    if(req === '/api/courses'){
        res.write(json.strigify([1,2,3]));
        res.end();
    }
});
server.listen(8000);
console.log('Listening @port 8000');

*


Solution

  • The strict comparison req === '/' is comparing a request object with a string, a comparison which will never be true.

    You likely meant to use: req.url === '/'