I'm very new to Node JS and would appreciate any help/advice.
Using NodeJS, I have created a local server run this link (it's simply a hello world)
From there, I have used this stack overflow snippet , which responds by the request type made.
I'd like to take this a step further, and when the reponse.method is get/post/etc. - I'd like the response to be a file.
In other words, I'd like to return a file from a HTTP response.
In theory, something along the lines of:
if(request.method == "GET")
{
response.end("C:\[file path]\test.txt");
}
I've tried simply copying/pasting the file directory after response.end('file directory') but was not opening the file that I wanted it to.
const http = require("http");
const hostname = '127.0.0.1';
const port = 8091;
var server = http.createServer ( function(request,response){
response.writeHead(200,{"Content-Type":"text\plain"});
if(request.method == "GET")
{
response.end("received GET request.")
}
else if(request.method == "POST")
{
response.end("received POST request.");
}
else
{
response.end("Undefined request .");
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});