Reputation: 2758
I can not understand createServer in Node.js This is the part of the code that makes me feel stupid
http.createServer((req, res) => {
let viewUrl = getViewUrl(req.url);
fs.readFile(viewUrl, (error, data) => {
if (error) {
res.writeHead(httpStatus.NOT_FOUND);
res.write("<h1>FILE NOT FOUND</h1>");
Handle errorswith a 404
}
This Incoming Message object, how do we know that it has .url method? From documentation
Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage
Where is req.url actually defined?
Upvotes: 0
Views: 593
Reputation: 708146
The callback to http.createServer()
is called when the request
event occurs on the server. That event is documented here and the first argument to that event is an http.IncomingMessage
object which is documented here. That object has a number of properties and methods including the .url
property described here.
Upvotes: 2
Reputation: 111
I didnt try what is the output of req.url. But it should come current url.
For your reference req is an object, which has url attribute. ie.
const req = {
url : ''
}
I will recommend you to use express js http://expressjs.com framework instead of node js core. So you can easily achive lot of things with minimal time.
Upvotes: 1