Guide Development Guide

hello, world

First, you need to successfully install fibjs in the system. By convention, we start our first line of fibjs program from the simplest "hello, world":

1
console.log('hello, world');

Save the file main.js later, at the command line fibjs main.js , you will be able to execute the code.

The first hello server

As a back-end development framework, fibjs has a very powerful application server built in. We can quickly build a web server very conveniently. Next we will create the first hello server:

1 2 3 4 5 6 7
const http = require('http'); var svr = new http.Server(8080, (req) => { req.response.write('hello, world'); }); svr.start();

After running, type in the browser:

1
http://127.0.0.1:8080/

You can see the output hello, world our first hello server.

Be smarter

The first hello server is stupid, no matter what address you enter, it will only return hello, world . Next, let's make it smarter:

1 2 3 4 5 6 7 8 9 10 11
const 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:

1
http://127.0.0.1:8080/fibjs

You can see that the output of the server has become hello, fibjs . When you modify the content of the address bar, the output of the server will also change accordingly.

Those who can do more, support static websites

Next, we let the server do some 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 13
const 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 file and putting it in for testing.

After running, we visit http://127.0.0.1:8080/hello/fibjs , we can still see hello, fibjs , and visit other addresses, we will see static files.

Increase module decoupling capability

Next, let's make the server more complicated. We have a set of hello services to complete the business requests we defined. The path of this group of services is specified by the main service according to demand. In the following example, both hello and bonjour 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 21
const 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 a completely decoupled module, and then use the main program to assemble the interface we need. This is particularly convenient in api version management, such as changing from /v1/hello/fibjs to /v2/hello/fibjs , the module itself does not need to be changed, just modify it at the entrance.

summary

Through this section, you can start your own fibjs server project. Next, we will introduce the development method of fibjs in more subdivided areas.

👉 [A good life starts from the test ]