Original post at toncho.dev
Express is a web application framework for Node.js that allows you to create APIs and web servers in a much easier and cleaner way. It is a lightweight package that does not obscure the core Node.js features.
Create Project
First, create a directory and then open it in the terminal
$ mkdir express-basic-server
$ cd express-basic-server
After that let's initializer the node project by running the command npm init
and answer a few questions
$ npm init
that's going to create a new file called package.json
with content very similar to
{
"name": "express-basic-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Now we will need to install express
, that's very easy, install it via npm
(node package manager) as you would with any other package
npm install express --save
that's going to update the file package.json
with the express
dependency and install it in the node_moudles
folder
{
"name": "express-basic-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.0"
}
}
Create Server
Create an HTTP server is very easy in Express. First, we need to create a file where we will set up our server, the most common names are app.js
and server.js
but you can call as you want
$ touch server.js
The first line is to import the express
package that we already installed, this will give us a module that is a function.
const express = require('express');
Then we need to create our app variable.
const app = express();
Note: You can create multiple apps this way, each with their requests and responses.
Now let's define the port that the server will be listening, it's a great idea to take that from the environment variables, so could be easily configurated and by default we will set it as 3000
const port = process.env.port || 3000;
At last, we must start our server! We are passing the port into the listen function. The function passed-in as the second optional parameter, and runs when the server starts up. This just gives us some feedback on the console to know that our application is running.
app.listen(port, () => {
console.log(`Server listening on port ${port}!`);
});
Now we have a basic server
const express = require('express');
const app = express();
const port = process.env.port || 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}!`);
});
To run the server let use the next command
$ node server.js
or
$ nodemon server.js
Note: If you would like to use
nodemon
you have to install it
The major difference between the node
and nodemon
command is that if you use node command you manually run the command each time after save but if you use nodemon
command it detects the changes automatically in any file of the project.
You can install nodemon
globally by running the command
$ npm install nodemon -g
Or you can install it as a dev dependency in the project and set a script to run the server with nodemon
$ npm install nodemon --save-dev
Now we have the package.json
file like
{
"name": "express-basic-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.0"
},
"devDependencies": {
"nodemon": "^1.19.0"
}
}
And we can run the server by running the command
$ npm run server
Basic Routing
The main responsibility of any server is how the application responds to the client request to specific endpoints like path by specific HTTP methods. In the server-side routing, each route has one route method, one route path, and one or more route handler functions.
Let's listen in our server to the path /
by the HTTP method get
and respond an <h1>
HTML tag with Hello World!
app.get('/', (req, res) => {
res.send('<h1>Hello World!</h1>')
});
Now our server.js
file like looks like
const express = require('express');
const app = express();
const port = process.env.port || 3000;
app.get('/', (req, res) => {
res.send('<h1>Hello World!</h1>')
});
app.listen(port, () => {
console.log(`Server listening on port ${port}!`);
});
There we have it, a basic Express server using Node.js. Now after running the server you can access to the http://localhost:3000 and see
You can find more info express
Top comments (0)