You know Javascript ? Write your first server with Node.js 💪

Work from anywhere, be a developer

This series is about teaching Léo to become a professional developer from scratch. Léo can be any of you, desiring to learn and grow 🚀

Learn by doing challenging projects, be mentored to achieve your goals and join a dedicated community ⚡

Apply here, it will be always be free ♥ https://mentorleo.co


If you followed the #week2 at MentorLéo, you already installed Node.js. You needed NPM, its package manager included with it, to manage your dependencies like a hero ! React, WebPack, Sass, Babel, …

Node makes Javascript live outside of a browser. Make scripts. Build servers.

Week 3 — Day 1

The hacker way

Write instructions with your terminal:

$ node
> var mentor = '@damien';
> console.log('My Mentor for this mentorleo.co project is ' + mentor);

The “>” indicates that you entered the Node execution loop. Tap Ctrl+C to exit the loop. This shortcut sends a killing signal, you can use it to kill almost every program running in your terminal.

You may never need to use this, it’s just background knowledge here :)

The sexy way

Create a hey.js file and write the same code:

Now run:

$ node hey.js

🙌

Build a server

We will build a server that handles HTTP requests. Your browser uses this protocol to fetch and submit data. You can see all the HTTP requests sent when you load a page in the Network panel of the Chrome DevTools. Try that for what you built with #week1 / #week2, or even for this page !

The 2 most common used “Verbs” or “Request Methods” HTTP request have are:
- GET: fetch data
- POST: submit data

If you want to go deeper in HTTP verbs, check-out REST:

Aside from HTTP, there are other protocols with which you can communicate. You’ll manipulate some in other projects at MentorLéo 😎

Express.js

THE easiest way to get started with HTTP server with Node.

$ npm install express

Create an app.js file that contains:

Launch 🚀

$ node app.js

In your browser, go to http://localhost:8080/hello

🙌

Decomposition

8080 is the port on which runs the server. Every server that uses any network protocol (HTTP or other) need a port to run onto. It’s like telephone numbers, 2 cellphones can’t use the same number at the same time.

/hello is what we call a route. HTTP servers expose routes to allow users to ask something (example: fetch data).

The result you see on the page is what we told the server to send when we hit that route /hello.

Route parameters

I want to you to build another route that uses a parameter.

If I go to

localhost:8080/hello/damien

I want the server to return “Hello Damien ! Nice to meet you 🙌” (with the first letter of the name in uppercase).

You can use this documentation: https://expressjs.com/en/guide/routing.html

Next day, we’ll see how to store data using databases \o/

→ click here to go to Day 2👍

Call to action

Join us ! We are creating a community of motivated Léos helping each other. Apply here, it will always be free ♥ https://mentorleo.co

Like what you read? Give Damien BRY a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.