PC Applications How to Install and Start Express JS Express.js is the most popular option when it comes to creating …

Express.js is the most popular choice when it comes to building web applications with Node.js. However, when saying web applications. ¿How to Install ExpressJS and Get It Up and Running?

It is often not visible at all in the browser (excluding server-side rendering). Instead Express.js, a web application framework for Node.js.

Allows you to create server applications in Node.js. As a background application, it is the glue between your frontend application.

A possible database or other data sources (REST API, GraphQL API, …). Just to give you an idea, the following is a list of technology stacks for creating client-server architectures.

How to Install ExpressJS and Get It Up and Running?

npm instalar Express

Now in your src / index.js file, use the following code to import Express.js, to create an instance of an Express application and to start it as an Express server:

importar exprimir desde 'expreso';

const aplicación = expreso();

aplicación.escucha(3000, () =>

 consola.log('Aplicación de ejemplo escuchando en el puerto 3000!'),

);

Once you start your application on the command line with npm startup, you should be able to see the output on the command line: Example application listening on Port 3000!

Your Express server is up and running. Everything that should happen after your Express app has started goes into the callback function.

The method itself takes another parameter as the first parameter, which is the port of the running application. That is why after starting it, the application is available via https: // localhost: 3000.

However, nothing should be available at this URL when you visit it in your browser.. We’ll get a sneak peek at this URL below before delving into it later.

Environment variables in Express.js

Before setting environment variables for your Node.js application. Let’s use an environment variable to set your port instead of coding it in source code.

If no such file exists, create a new .env file in your project. Otherwise, use the .env file that is already there. Give it a new key value pair to define your port:

PORT=3000

Now in your src / index.js file, import the node package that makes the environment variables available in your source code and use the environment variable PORT to start your Express application.

importar 'dotenv / config';

importar cors desde 'cors';

importar exprimir desde 'expreso';

const aplicación = expreso();

aplicación.utilizar(cors());

aplicación.obtener('/', (Req, res) => {

 res.enviar('Hello World!');

});

aplicación.escuchar(proceso.env.puerto, () =>

 consola.log(`Aplicación de ejemplo que escucha en el puerto $ {process.env.PORT}!`),

);

Instead of exposing the port that is used in the source code, you have stored it in a more sensible place in your environment variables.

If you are using Git with something like GitHub, you can exclude it from being uploaded to the GitHub repository by adding it to your .gitignore file.

This is how sensitive data is kept out of public Git repositories like GitHub. If you deploy your application in production eventually.

You can add the environment variables as a .env file on your web server that is serving your application.