Node.js is an open source, cross-platform avaScript runtime environment that can run JavaScript on the server side. Node.js is owned and maintained by the Node.js Foundation and has a cooperative relationship with the Linux Foundation. Node.js uses the V8 running code developed by Google, and uses event-driven, non-blocking and asynchronous input and output models to improve performance and optimize the transfer volume and scale of the application. These techniques are often used in data-intensive real-time applications. Most of the basic modules of Node.js are written in JavaScript. Before Node.js appeared, JavaScript was usually used as a client-side programming language, and programs written in JavaScript were often run on the user's browser. The emergence of Node.js enables JavaScript to also be used for server-side programming.
Therefore, learning to try Node.js is very useful. This article outlines the steps required to run "Hello world" in Centos running node.js + express.
First of all, we need a CVM (the smallest is enough), if it’s just an experiment, I recommend you to use Tencent Cloud free Developer Lab for use, and a SSH clients (such as Putty on Windows, SSH on Linux systems and Mac OSX, which are usually built-in). When we receive the initial root password, we can use ssh to apply it to the instance. Use SSH to enter CVM and change the root password. It is best to update the software repository to the latest version:
yum -y update
This will update the software installed on our CVM to the latest version. Yum may take a few minutes. When it is finished, we need to prepare the software for installation. We will build Node.js from the latest source code. At the time of writing this article, the latest version is v0.10.4. For this, we need "development tools". It is a set of tools for compiling software from source code.
yum -y groupinstall "Development Tools"
This command will extract the "Development Tools" group with the application required to compile node.js.
In addition, we will install screen
, a software that can make your SSH background work. It is very convenient, especially when developing and compiling.
yum -y install screen
Now we are ready to install Node.js from source. First, we will move to the /usr/src directory, which is usually used to store software sources.
cd /usr/src
Now, we select the latest compressed source archive from the Node.js website http://nodejs.org/download.
wget http://nodejs.org/dist/v0.10.4/node-v0.10.4.tar.gz
If there is a new version, we recommend that you replace the version in the url. Next, we unzip the source file and enter the directory.
tar zxf node-v0.10.4.tar.gz
cd node-v0.10.4
Now extract the source code of Node.js, we are in the source directory. We can now prepare our compiler commands by executing the configure script:
. /configure
This will read the properties of our system to prepare the contents of the compiler, for example, it will read your system architecture (32/64 bit, CPU, etc.). With it, we are now ready to actually compile the source code. Just enter:
make
This is probably the most time-consuming task: on CVM, it takes about 6 minutes and 34 seconds to complete. When we are finished, we can use the installation:
make install
The latest command puts the compiled binary file in the system path, so all users can use it without further settings. By default, node binaries should be installed in /usr/local/bin/node
.
We have now installed and completed Node.js, we can start development, deploy the completed application, or we can install the Express.js web framework. First, we will use the node module manager (npm) to quickly install plugins and management programs. This is a very useful module that can keep our application started, monitor file changes (for example, when developing an application) and when needed Restart CVM.
npm -g install express express-generator supervisor
npm -g install
will install the express
and supervisor
modules from the npm software repository and make them available to the entire system. The -g
flag in this command means "global".
For security reasons, you should now create a regular system user and run the node under an unprivileged account.
To do this, please add users first. You can replace "exampleuser" with any name you like.
useradd exampleuser
Now we have a new system user. Add a suitable password for the new user:
passwd exampleuser
**Log out, and then log back in as the new user. **
This will change our login shell from root (system user) to exampleuser (non-privileged user, which can destroy the system with less damage).
Express is a powerful framework. To create our first application, all we have to do is enter:
express hello
This command will create a "hello" directory and make some basic settings for the new application. Now we should go into this directory and install express dependencies:
cd hello && npm install
The npm install
command will read all module dependencies from the generated package.json
file and install it from the npm software repository. We should start a new screen session so that our node can run:
screen
Finally, we can start our application.
supervisor ./bin/www
Now, we can access our first express application through your CVM IP. For example, http://123.456.78.90:3000
.
Have you learnt that? For more Nodejs basic tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn.
Reference: "How To Install And Run A Node.js App On Centos 6.4 64bit"
Recommended Posts