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.jslater, at the command line fibjs main.js, you can execute the code.

The first hello server

As a back-end development framework, fibjs has a very powerful application server built in, and we can quickly build a web server very conveniently. Next we will set up 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 of our first hello server hello, world.

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 changed 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 told the server to do something, we want the server to support a static file browser, but can also output hello, world, we set the response hello, fibjsaddress is: /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 weband store some files in it, such as downloading a fibjs file and putting it in for testing.

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

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 requirements. The example below, helloand bonjourwill be directed hello services.

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 especially convenient api version management, such as the /v1/hello/fibjsmodification to /v2/hello/fibjsthe module itself does not need to make any changes, you can modify only 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 ]