More and more web applications are using ExpressJS. It’s an easy-to-use and powerful web framework for Node.js. What we’re learning today is how to set up a basic Express.js website.
Prerequisites:
- Node.js with NPM installed on your system
Let’s start!
First of all, create a new project folder. You can do this manually or with the following commands:
If you’ve done, you should create a package.json using the command npm init in the console. If everything has worked fine, a new file should have been created called package.json . In this file, all project settings are saved like all dependencies the project needs, what the main file is, … You can read more about what the package.json is here.
Now it’s time to install express.js. You can be doing this with the following command: npm install express --save With this command you install expressjs in your project folder and save it into your package.json .
As in nearly every beginner tutorial there is a “Hello World” example that we’ll create now. Create a new file in your root folder (In this example it should be in ./myNewApp) called app.js . In this file we’ll setup express.js.
If you’ve successfully created this file, you can start entering the following code:
In the first line we use require() which lets Node.js know that we need express for this file to work. In the second line we create a new web server using the express() function. In the third line we set the port which can be whatever you want.
You can start your web application using the command node app.js . As you can notice, there is no output in the console. This means you have no response if the web application is running now or not. There’s an easy solution for that. Just add the following lines below your code from before:
If you restart your express server now, you will see that in the console is written “My first Express.js web application listening at http://localhost:80”
Now your web server is running on port 80 but you don’t have any routes yet. Add the following 3 new lines to your code in your app.js :
In the first line we define that the web application should listen to every GET request which is coming from / . This means that if you access your website locally using localhost:80/ the web application will listen to it and execute the code which is on line 2. On line 2 we define that whenever an user uses the URL localhost:80/ , then the response ( res ) should send() “Hello World!” to the client. The result will be the following:
HURRAY! Your first web application with express.js is running! Now you can create as many routes as you want. You have so much you can do with express.
Here are some websites where you can learn a lot about express:
- https://expressjs.com/
- https://expressjs.com/en/resources/learning.html
- https://developer.mozilla.org/en/docs/Learn/Server-side/Express_Nodejs
- https://www.tutorialspoint.com/expressjs/index.htm
Conclusion
I hope I could help you a bit setting up your first express.js web application. I am using express for many web application I’ve made so far, and not only for private projects but also for business projects. If you have any questions just comment below!
Happy coding!
Top comments (0)