Node Express
Node.js
Node.js is an interpreter capable of executing javascript code on the server side. The Node.js architecture is based on asynchronous functions, with which developers are now able to create more dynamic web pages and work with more scalability on projects.
How to use node
Running a node.js server
Download the latest vesion of Node.js
Once installed, create a api.js file with this content.
//imports const http = require("http"); // the host of the aplication (localhost or the users machine ip address) const hostname = "localhost"; // the port that the server is going to run (3000 is the default port) const port = 3000; // the createServer method creates a server on your computer const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader("Content-Type", "text/plain"); res.end("Hello World"); }); // listen to the server stance server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Now run the file typing
node api.js
on terminal and access localhost:3000 to see the server runing.
Express
Express is a node.js based framework that allows developers to create web applications and api's written in javascript.
How to use express
Running an express server
First make sure you have npm install.
Create a new empty project.
- Run
mkdir myapp
thencd myapp
. - Within the folder run
npm init
to create a package.json file, you will be asked some infos about the project, for now just keep typing enter.
- Run
Now with the package.json created, install the express dependency by runing
npm install express --save
inside of the project.Create the
index.js
file inside the project, this file will be our entry point for the project.//imports const express = require("express"); //Starting the express server. const app = express(); // the port that the server is going to run (3000 is the default port) const port = 3000; // creating a route app.get("/", (req, res) => res.send("Hello World!")); // listen to the server stance app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`) );
Now run the file typing
node index.js
on terminal and access localhost:3000 to see the server runing.
How we use today
We use node.js and Express in our backend and Sequelize to make the connection with the db.
The entry point of the server is in src/index
, with that we use a folder structure to keep the code clean and organized.
- /cron - All cronjobs of the project.
- /middlewares - All middlewares of the project.
- /models - All models of the project, each model has his on file with his correspondent methods.
- /routes - All routes of the project, most of the routes name can be organized by model.
- /schemas - All schemas of the project, schema can be understand as the representation of the model on database.
- /scripts - All scripts of the project.
- /typings - All typings of the project.
- /utils - All files that can provide useful on different parts of the project.
Where to learn
- Nodejs Docs - Getting start guide.
- Expressjs Docs - Getting start guide.
- Express Routing - Documentation of how to use the routes.
- Node.js Ultimate Beginner’s Guide - Guide explaining what is nodejs and express and how to create a server.
- Introduction to Node & Express - Article to help
Practice
- Create a node.js project with Express and create one route each http requests (get, post, put, delete) and test using an API client tool (like Insomnia or Postman)