In this post we will install Express, the node.js framework, and we will create our first project with the famous "Hello World!"
We will first explain what is Express:
"Express is a minimal and flexible node.js web application
framework providing a robust set of features for building
single and multi-page, and hybrid web applications."
It's a short description, but I think nobody can say better. Just saying it's a framework for Node.js and we will save much time and labor.
Install Express
We need to have installed Node.js. If you have not yet installed in this post explains how you have to do step by step:
Then we need open the terminal and write:
$ npm install -g express
Npm Node.js is an assistant to help with the installation of the packages, we have just done is to install the package Express on a global basis.
Easy!! Rigth?? Now go to create our first project in Express and Node.js.
Create a Project with Express and Node.js
We need open de terminal again and:
1 - Find the desired location.
2- Inside the folder in the terminal write:
$ express helloworld
The project was created, and the terminal shows the folders that you created and how to continue the installation.
3 - Install dependencies. The dependencies are external Node.js modules that needs to work correctly. Write in the terminal:
$ cd helloworld && npm install
4 - If we start the server we can see our proyect in the browser. Write in the terminal:
$ node app
And now you can open the browser and go to this url: localhost:3000/
Create the HelloWorld!!
Now open the folder of your project and we made the following changes:
1 - Open the file App.js and add "app.get('/helloworld', routes.helloworld); " under this other two (line 33):
2 - Open the file /routes/index.js and under all add this function:
exports.helloworld = function(req, res){ res.render('helloworld', { title: 'HelloWorld' }); }; |
3 - Create our view in Jade. First open the file /views/index.jade and create a copy called "helloworld.jade". Open the new copy and delete all code inside and write:
extends layout block content h1= title p This is my First #{title} in Express and NodeJS |
4 - Finally, you start the server again with:
$ node app
And you can open the browser and go to this url: localhost:3000/helloworld
I hope you liked it, and if you have any questions feel free to write me
No comments:
Post a Comment