📜 ⬆️ ⬇️

Web development on node.js and express. We study node.js in practice

Foreword



A bold idea came to my mind, to start writing a tutorial on developing on node.js (something like ruby.railstutorial.org ). Since this is the first time I undertake such work, any feedback is very important for me. Need it - not necessary. What should be removed and what to dwell upon, in general, any constructive criticism. I hope that it will go and you will like it, in this case, as I write, I will lay out chapters on Habr.

Introduction


')
Greetings, in front of you is a small tutorial on practical development on node.js, using the express framework. I am very enthusiastic about node and related technologies. Node.js primarily attracts freshness in development approaches, courage and drive.

You can read about node.js at http://nodejs.org/ , in short, this is a server platform for javascript execution. We will also use express, a web-framework built on the concept of middleware (we’ll talk about it in more detail later)

In the process of learning, we will learn about various aspects of web development, such as using a version control system, automatic testing, and so on. As a result, in the course of the study, we will develop a completely working web-application (a simple analogue of Twitter)

I would like to note that the railstutorial had a great influence on me, this is the best manual on web development that I have met, and I would very much like to create something similar for node.js.

Chapter 1. Start



In this chapter, we will deploy the working environment from scratch, install all the necessary tools, create a simple application and even run it on a cloud hosting.

1.1 Work environment



Since I mainly use linux in my work, and more specifically, Ubuntu 12.04, most of the instructions for installing these or other tools will be aimed at Linux users, but I will try to give references to instructions for other OSs as much as possible.

In order to follow the instructions in the tutorial exactly, you will need to install a git version control system, the fact is that we will not only place the code of our projects in git, but also install many of the tools from the repositories on the github .

So the first thing we do is ...

1.1.1 Install git



Users of apt-based distributions can run in the terminal:

$ sudo apt-get install git-core 


The rest are sent to read the instructions at http://git-scm.com/book/ch1-4.html

1.1.2 Installing node.js and npm



Now it's time to install the latest stable version of node.js and npm (package installer for node). Instructions for installing different operating systems can be found here . To install on ubuntu we run:

 $ sudo apt-get install python-software-properties $ sudo add-apt-repository ppa:chris-lea/node.js $ sudo apt-get update $ sudo apt-get install nodejs npm 


If there is a desire - you can run the node console and play with the javascript interpreter.

1.1.3 Development Environment



Here everyone is free to choose according to his taste, personally, I am completely satisfied with gedit with the set of gmate plugins installed . Netbeans or Webstorm are suitable.

1.1.4 Express and first application



Now it's time to get acquainted with the express framework. The framework is very simple, and quite acceptable documented.

Install express globally:

 $ sudo npm install -g express 


Create a directory for our training projects:

 $ mkdir -p ~/projects/node-tutorial $ cd ~/projects/node-tutorial 


Create a project and install dependencies:

 $ express first-app $ cd first-app && npm install 


Those interested can delve into the fact that we have generated an application generator, I think that people familiar with javascript can assume what is happening there.

Now you can run the application:

 $ node app 


And see the result of http: // localhost: 3000 /

1.2 Version Control System



Now that we already have a working application, let's touch on working with the version control system in more detail. In order to get better acquainted with the work of git, you should read the book Pro Git , but you can get by with the instructions in this tutorial.

1.2.1 Configuring git



For more comfortable working with git, you should first enter your personal data:

 $ git config --global user.name "Your Name" $ git config --global user.email your.email@example.com 


And set up aliases for the most frequently used commands:

 $ git config --global alias.co checkout $ git config --global alias.ci commit 


1.2.2 Working with git



Git is configured and you can place our application in the repository, initialize the new repository:

 $ git init 


Add a directory with application dependencies in gitignore:

 $ echo 'node_modules' > .gitignore 


We place all the files in the index and create the first commit:

 $ git add . $ git ci -m "Initial commit" 


1.2.3 GitHub



After placing the project code in the repository, it's time to post the project on GitHub . GitHub is a social network and hosting for projects. A huge number of opensource projects are hosted on a githaba, so if you are not registered there yet, it's time to do it .

Before working with GitHub, you will need to create RSA keys for ssh access. The procedure is described here . For linux users, I provide instructions on how to create keys if you do not already have them.

 $ ssh-keygen -t rsa -C "your_email@youremail.com" 


We answer the generator's questions, and then copy the contents of the file ~/.ssh/id_rsa.pub :

 $ sudo apt-get install xclip $ xclip -sel clip < ~/.ssh/id_rsa.pub 


After that, go to the Account Settings link, go to the SSH Keys section and click the Add SSH Key button and paste the key from the clipboard into the Key field. Then save.

You can verify that the key works like this:

 $ ssh -T git@github.com 


You might see a warning:

 The authenticity of host 'github.com (207.97.227.239)' can't be established. # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. # Are you sure you want to continue connecting (yes/no)? 


You just need to answer 'yes' and then, if the key was successfully added, you will see the server response:

 Hi username! You've successfully authenticated, but GitHub does not # provide shell access. 


When the keys are configured, we create a new repository with the name first-app and default settings, after which we post the code on the githab:

 $ git remote add origin git@github.com:___/first-app.git $ git push -u origin master 


1.3 Deploy the application



Now comes the most exciting stage, we will deploy the application on the hosting.
To do this, we use the services of the Heroku cloud deployment system. If you're wondering how Heroku hosting works, I advise you to study their How How Works section.

1.3.1 Configure Heroku



First we need to register and install the necessary tools .

Ubuntu users perform:

 $ wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh 


When the installation is complete, you will need to login from the command line:

 $ heroku login 


1.3.2 Hosting the application on heroku



Now our environment is completely ready for hosting. Placing a node.js project on Heroku requires a few more steps, you can read about it in the documentation or simply follow the instructions.

In the package.json file of our project, you need to specify the version of the node and npm, package.json should look like this:

 { "name": "application-name", "version": "0.0.1", "private": true, "scripts": { "start": "node app" }, "dependencies": { "express": "3.0.x", "jade": "*" }, "engines": { "node": "0.8.x", "npm": "1.1.x" } } 


Now in the root of the project we create the Procfile file:

 $ echo 'web: node app.js' > Procfile 


We check that everything is started using the process manager:

 $ foreman start 


The application must be available at http: // localhost: 5000 /

Add files to the repository:

 $ git add . $ git ci -m "Added Procfile and engines" $ git push 


Create an application on heroku:

 $ heroku create $ heroku keys:add ~/.ssh/id_rsa.pub $ git push heroku master 


We collect:

 $ heroku open 


and admire the application.

Source: https://habr.com/ru/post/146983/


All Articles