Node.js First steps, don’t know how to start

Asked

Viewed 138 times

-1

I’ve been dedicating myself to Javascript for some time, looking for various ways to learn, recently I came across the callback functions, usually used when using javascript on a server instead of local (correct me if I’m wrong).

I found a series of exercises to learn how to simulate a server with Ode, downloaded all the files and now I intend to start programming on it. However the exercises I am following to learn step by step, it is not clear to me yet, follow what is suggested:

Exercício de Node JavaScript

What he means in "the server must respond to /file/* URI? And then map to a Folder location. I can’t understand what I should do to start working with this. If someone could give a clearer explanation, so that a beginner could understand and develop something, or even a generic code as an example, it would also be very productive to the studies. Thank you very much.

All I’ve done so far is:

 var http = require('http');

function onrequest(request, response) {
  response.writeHead(200);
  response.end("Hello World\n");
}

var server = http.createServer(onrequest);
server.listen(8000);

console.log("Listening on http://127.0.0.1:8000/");
  • Here’s an example that can help https://adrianmejia.com/building-a-node-js-static-file-server-files-over-http-using-es6/

  • Excellent for Nodejs beginners: https://www.youtube.com/playlist?list=PLJ_KhUnlXUPtbtLwaxxUxHqvcNQndmI4B

1 answer

1


This onrequest function will be called each time there is a request to the server.

You have two parameters in that function, the first is the object of the request (request) and in it you have a property called url. This means that when the server receives a request it will call this function and pass as argument an object request where you can read the request.url.

For example: if you call console.log(request.url); inside that function and in the browser you put http://localhost:8000/file.text1.js you will see in the terminal the requested url or /file.text1.js.

Then you can put logic to filter the routes you want with Regexp. For example, if the route starts with /file/ go read a file to a specific directory can be done like this:

const http = require("http");
const fs = require("fs");

const rootFolder = __dirname;

function onrequest(request, response) {
  console.log(request.url);
  const isFileFolder = request.url.match(/^\/file\/*/);
  if (isFileFolder) {
    const fileName = request.url.slice(5); // remover "/file"
    const data = fs.readFileSync(rootFolder + fileName, "utf8");
    response.writeHead(200);
    response.end(data);
    return;
  }

  response.writeHead(200);
  response.end("Hello World\n");
}

const server = http.createServer(onrequest);
server.listen(8000);

console.log("Listening on http://127.0.0.1:8000/");

I tested this example that I wrote as follows: I put this code inside a file called server.js and then in the browser called http://localhost:8000/file/server.js. This returned the contents of server.js.

Now you just need to adapt to your specific case and change the value of rootFolder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.