Let's make a web application program by using server side JavaScript.
We generate a container. The web application written in Node.js runs in the container.
Node.js is a platform including the engine of JavaScript running on server side.
Programs written in Node.js are light and efficient, and can handle a lot of requests from network.
Moreover, functions of file and network I/O, etc. are added.
Web applications created by Node.js are different from the web applications which are composed of the language like PHP, Java, Perl, etc. and the web server such as Apache, Nginx, etc.
Programs written in Node.js can handle http requests. They work as reliable and efficient web servers.
However, it is hard to make an advanced web server from scratch. To create advanced web servers easily, there are various web application frameworks in Node.js. Let's use Express.js here.
Let's obtain the source.
The web site of Node.js is the following link.
http://nodejs.org/
Click DOWNLOADS button.
Click the link of "Source Code". Then, you can download a tar archive.
Please install gcc if it is not installd.
gcc is necessary for the compilation.
# yum install gcc
Extract the tar archive to home directory.
$ tar zxvf node-v0.10.26.tar.gz
Enter the extracted directory.
$ cd node-v0.10.26
Execute configure command.
$ ./configure
Execute make command.
$ make
Become root user and install it.
# make install
Thanks to Node.js, JavaScript can be used like average script languages on the command line.
The command name of Node.js is node. The command is placed under /usr/local/bin if you installed from the source code.
$ls -l /usr/local/bin/node
-rwxrwxr-x. 1 root root 11078072 Mar 5 08:57 /usr/local/bin/node
Let's display "hello, world" first.
Please save the following program as hello.js.
console.log('hello, world!');
Let's execute it.
$ node hello.js
hello, world!
Let's try another one.
The next program calculates Fibonacci numbers.
Please save the following program as fib.js.
function fib(n) {
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fib(n-1)+fib(n-2);
}
}
for (var i = 1; i < 10; i++) {
console.log(fib(i));
}
Let's execute it.
$ node fib.js
1
1
2
3
5
8
13
21
34
Execution will end in a short time.
Node.js adds the functions of file I/O and network I/O to ordinary JavaScript. It also has the features of blocking I/O and event loop, which are helpful to make web applications robust against high load.
However, it is still hard to make advanced web applications, which have common web server functionality like apache, from scratch. To create such web applications easily, the library collecting packages of various applications and frameworks which can be used on Node.js is maintained on the internet.
When Node.js is installed, a tool named npm is installed at the same time. Let's see under /usr/local/bin.
$ ls -l /usr/local/bin/npm
lrwxrwxrwx. 1 root root 38 Mar 5 09:41 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
This is a tool that treats packages of Node.js, like apt, yum and so on.
You can find various maintained packages from the following link.
npm Node Packaged Modules
https://www.npmjs.org/
Let's install the framework of the Web application of Node.js by npm.
Note) If you need to use proxy to access the internet, you can set npm to use proxy as follows.
# npm config set proxy http://proxyURI:port
There are various frameworks for web applications of Node.js.
Express.js is very popular from them. We use it here.
Installing Express.js is easy by using npm.
Please execute the following command.
# npm -g install express
Required packages are downloaded and installed.
# npm -g install express
npm http GET \https://registry.npmjs.org/express
npm http 304 \https://registry.npmjs.org/express
npm http GET \https://registry.npmjs.org/connect/2.14.1
npm http GET \https://registry.npmjs.org/commander/1.3.2
npm http GET \https://registry.npmjs.org/mkdirp/0.3.5
npm http GET \https://registry.npmjs.org/range-parser/1.0.0
npm http GET \https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET \https://registry.npmjs.org/cookie/0.1.1
npm http GET \https://registry.npmjs.org/fresh/0.2.2
Omissionnpm http 304 \https://registry.npmjs.org/debuglog/0.0.2
npm http 304 \https://registry.npmjs.org/core-util-is
/usr/local/bin/express -> /usr/local/lib/node_modules/express/bin/express
express@3.5.0 /usr/local/lib/node_modules/express
├── methods@0.1.0
├── merge-descriptors@0.0.2
├── cookie-signature@1.0.3
├── range-parser@1.0.0
├── fresh@0.2.2
├── debug@0.7.4
├── buffer-crc32@0.2.1
├── cookie@0.1.1
├── mkdirp@0.3.5
├── commander@1.3.2 (keypress@0.1.0)
├── send@0.2.0 (mime@1.2.11)
└── connect@2.14.1 (response-time@1.0.0, pause@0.0.1, connect-timeout@1.0.0, method-override@1.0.0,
static-favicon@1.0.0, vhost@1.0.0, qs@0.6.6, basic-auth-connect@1.0.0, morgan@1.0.0, serve-
static@1.0.2, bytes@0.2.1, raw-body@1.1.3, errorhandler@1.0.0, csurf@1.0.0, cookie-parser@1.0.1,
compression@1.0.0, express-session@1.0.2, serve-index@1.0.1, multiparty@2.2.0)
Well, let's execute Node.js and Express.js installed here in a container.
From the next section, we generate a container by using LXCF, and create a web application using Express.js in the container.
We can also create two or more containers. It is possible to execute other web applications in each container.
Well, let's make a container here.
Please install LXCF according to the LXCF Install beforehand.
Execute the following command with root previlege.
# lxcf sysgen a-srv
"a-srv" is a container name here. Alphanumeric character, "-" and "_" can be used for the container name.
After the container creation has done, we can see the container a-srv's information by "lxcf list" command.
# lxcf list
Name Mode State Path
a-srv joint running /opt/lxcf/a-srv
You can login with ssh in the container.
# ssh a-srv
[root@a-srv ~]#
The shown prompt is indicating that we are inside the container.
If you cannot login with ssh, please wait for a few minutes, and retry after that because starting ssh server might take a few minutes.
In the a-srv container made here, we can use the all the software installed in the host.
However, the container has an individual ip address like virtual machines.
Please login with ssh and try something. Unlike virtual machines, you'll feel almost no overhead and latency.
The erase command is used as follows to delete the container.
# lxcf erase a-srv
erase /opt/lxcf/a-srv
erase /etc/lxcf/rsc/a-srv
erase ip address from /etc/hosts
erased a-srv
In LXCF, the network configuration by default is NAT for the control connection.
It is necessary to add the setting of the network to provide the network service outside.
Because LXCF uses libvirt-lxc, we can use virt-manager or virsh to add the network setting.
Please refer to the manual of virt-manager and virsh for the method of the setting.
It works as follows in this paragraph.
1) Making of LXC container: Container name express-srv
2) login by ssh
3) A new user who operates express is made: User-name express
4) Generation of model application program of express: Application program name lxcfapp
5) The access is confirmed from HOST by a web browser
First of all, the LXC container that operates the Express application program is made. Here, express-srv is assumed to be a container name.
# lxcf sysgen express-srv
Generating express-srv...
creating /opt/lxcf/express-srv...
creating /opt/lxcf/express-srv/proc...
creating /opt/lxcf/express-srv/etc...
creating /opt/lxcf/express-srv/run...
creating /opt/lxcf/express-srv/opt...
creating /opt/lxcf/express-srv/home...
creating /opt/lxcf/express-srv/root...
creating /opt/lxcf/express-srv/boot...
creating /opt/lxcf/express-srv/var...
......................
creating /opt/lxcf/express-srv/usr...
creating /opt/lxcf/express-srv/sbin...
creating /opt/lxcf/express-srv/bin...
creating /opt/lxcf/express-srv/lib...
creating /opt/lxcf/express-srv/lib64...
Setup completion of LXCF
Domain lxcfapp defined from /opt/lxcf/lxcfapp/etc/lxcf/rsc/lxcfapp/lxcfapp.xmlexpress-srv is starting
The system in the container boots it when the LXC container is made, and it waits for 1 or 2 minutes.
The sshd demon be able to start automatically, and come to do login a new container in ssh.
# ssh express-srv
[root@express-srv ~]#
The container name is displayed in the prompt. Ssh login was able to be done.
The following register the user for the Web application program, and switch the work user.
[root@express-srv ~]# useradd express -m
[root@express-srv ~]# su - express
[express@express-srv ~]$
The express user who made it here is effective only in the container. It is a user who does not exist on HOST. Even if the express application program of node.js is newly made, it is a peculiar application program to the container. It is not on HOST.
Nodejs is installed on HOST in the preceding chapter.
The program installed on HOST can be used as it is also on the container newly made in LXCF.
Well, let's make the model of express.
The model of the application is made from the name of lxcfapp here.
The lxcfapp directory is made, and the file of the model is arranged under that.
[express@express-srv ~]$ express lxcfapp
create : lxcfapp
create : lxcfapp/package.json
create : lxcfapp/app.js
create : lxcfapp/public
create : lxcfapp/public/javascripts
create : lxcfapp/public/images
create : lxcfapp/public/stylesheets
create : lxcfapp/public/stylesheets/style.css
create : lxcfapp/routes
create : lxcfapp/routes/index.js
create : lxcfapp/routes/user.js
create : lxcfapp/views
create : lxcfapp/views/layout.jade
create : lxcfapp/views/index.jadeinstall dependencies:
$ cd lxcfapp && npm installrun the app:
$ node app
Next, it moves to the lxcfapp directory, and "Npm install" is executed.
[express@express-srv ~]$ cd lxcfapp
[express@express-srv lxcfapp]$ npm install
npm http GET \https://registry.npmjs.org/express/3.5.0
npm http GET \https://registry.npmjs.org/jade
npm http 200 \https://registry.npmjs.org/express/3.5.0
npm http GET \https://registry.npmjs.org/express/-/express-3.5.0.tgz
npm http 200 \https://registry.npmjs.org/express/-/express-3.5.0.tgz
npm http 200 \https://registry.npmjs.org/jade
npm http GET \https://registry.npmjs.org/jade/-/jade-1.3.1.tgz
npm http 200 \https://registry.npmjs.org/jade/-/jade-1.3.1.tgz<omission on="" the="" way=""></omission>
├── mkdirp@0.3.5
├── commander@1.3.2 (keypress@0.1.0)
├── send@0.2.0 (mime@1.2.11)
└── connect@2.14.1 (response-time@1.0.0, pause@0.0.1, method-override@1.0.0, connect-timeout@1.0.0, > > vhost@1.0.0, static-favicon@1.0.0, qs@0.6.6, basic-auth-connect@1.0.0, morgan@1.0.0, serve->static@1.0.2, bytes@0.2.1, raw-body@1.1.3, errorhandler@1.0.0, csurf@1.0.0, cookie-parser@1.0.1, express-session@1.0.2, compression@1.0.0, serve-index@1.0.1, multiparty@2.2.0)
When install ended, the program of JavaScript named lxcfapp directory under app.js was generated.
This app.js is a main body of the application.
[express@express-srv lxcfapp]$ ls
app.js node_modules package.json public routes views
Then, let's execute the model of the generated lxcfapp application.
app.js is executed by the node command.
[express@express-srv lxcfapp]$ node app.js
Express server listening on port 3000
Listen is done with port 3000. "Http://express-srv:3000" should be able to be accessed from a browser such as firefox by this on HOST.
However, please wait for a moment. Perhaps, port 3000 is blocked in the firewall that operates in the LXC container.
Let's release the block of port 3000 of firewall in the container. Naturally, this operation does not influence the firewall of HOST.
It interrupts by control C(^c). It returns to root. And, command (firewall-cmd) that releases the block of firewall is executed.
Let's return to former place when the block is released.
^c
[express@express-srv lxcfapp]$ exit
[root@express-srv ~]# firewall-cmd --permanent --add-port=3000/tcp
success
[root@express-srv ~]# su - express
[express@express-srv ~]$ cd lxcfapp
[express@express-srv lxcfapp]$
The application program model that executes "node app.js" and makes it is executed again.
[express@express-srv lxcfapp]$ node app.js
Express server listening on port 3000
It accesses "\http://express-srv:3000" by starting firefox on HOST.
How?Did you look like above?
The address of express-srv can be accessed only from among the NAT environment of HOST. To access it excluding HOST, the composition of the network should be added and be changed.