hello, world
First, you need to successfully install fibjs in your system. As usual, we start our first line of fibjs program with the simplest "hello, world":
1console.log('hello, world');
After saving the file main.js
, fibjs main.js
you can execute this code by typing it on the command line.
The first hello server
As a back-end development framework, fibjs has a built-in quite powerful application server. We can quickly build a web server very conveniently. Next we will set up our first hello server:
1
2
3
4
5
6
7const http = require('http');
var svr = new http.Server(8080, (req) => {
req.response.write('hello, world');
});
svr.start();
After running, enter in the browser:
1http://127.0.0.1:8080/
You can see the output from our first hello server hello, world
.
Be smarter
The first hello server is stupid, it just returns no matter what address you enter hello, world
. Next let's make it a little smarter:
1
2
3
4
5
6
7
8
9
10
11const http = require('http');
var hello_server = {
'/:name': (req, name) => {
req.response.write('hello, ' + name);
}
};
var svr = new http.Server(8080, hello_server);
svr.start();
After running, enter in the browser address bar:
1http://127.0.0.1:8080/fibjs
You can see that the output content of the server changes hello, fibjs
. When you modify the content of the address bar, the output content of the server will also change accordingly.
Those who can do more work, support static websites
Next, we ask the server to do more things. We hope that the server can support static file browsing and can also output hello, world
. We set the response hello, fibjs
address as: /hello/fibjs
:
1
2
3
4
5
6
7
8
9
10
11
12
13const http = require('http');
const path = require('path');
var root_server = {
'/hello/:name': (req, name) => {
req.response.write('hello, ' + name);
},
'*': path.join(__dirname, 'web')
};
var svr = new http.Server(8080, root_server);
svr.start();
You need to create a directory web
and store some files in it, such as downloading a fibjs document and placing it in it for testing.
After running, we http://127.0.0.1:8080/hello/fibjs
can still see it when we access it hello, fibjs
, but when we access other addresses, we will see static files.
Increase module decoupling capabilities
Next, let's make the server a little more complex. We have a set of hello services that complete the business requests we define. The path to this set of services is specified by the master service based on requirements. In the following example, hello
and bonjour
will point to the hello service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21const http = require('http');
const path = require('path');
var hello_server = {
'/:name(fibjs.*)': (req, name) => {
req.response.write('hello, ' + name + '. I love you.');
},
'/:name': (req, name) => {
req.response.write('hello, ' + name);
}
};
var root_server = {
'/hello': hello_server,
'/bonjour': hello_server,
'*': path.join(__dirname, 'web')
};
var svr = new http.Server(8080, root_server);
svr.start();
In this way, we can easily create completely decoupled modules, and then use the main program to assemble them into the interfaces we need. This is particularly convenient in api version management. For example, if you /v1/hello/fibjs
change from to /v2/hello/fibjs
, the module itself does not need to make any changes, just modify it at the entrance.
summary
Through this section, you can start your own fibjs server project. Next, we will introduce the development methods of fibjs in more subdivisions.