UNPKG

4.69 kBMarkdownView Raw
1# **server.js** for Node.js
2
3[![Subscribe](https://img.shields.io/badge/%20subscribe%20-%20mailchimp%20-blue.svg )](http://eepurl.com/cGRggH)
4[![Downloads](https://img.shields.io/npm/dm/server.svg)](https://npm-stat.com/charts.html?package=server)
5[![Status](https://github.com/franciscop/server/workflows/tests/badge.svg)](https://github.com/franciscop/server/actions) [![Dependencies status](https://david-dm.org/franciscop/server/status.svg)](https://david-dm.org/franciscop/server)
6
7Powerful server for Node.js that just works so **you can focus on your awesome project**:
8
9```js
10// Include it and extract some methods for convenience
11const server = require('server');
12const { get, post } = server.router;
13
14// Launch server with options and a couple of routes
15server({ port: 8080 }, [
16 get('/', ctx => 'Hello world'),
17 post('/', ctx => {
18 console.log(ctx.data);
19 return 'ok';
20 })
21]);
22```
23
24<blockquote class="external">
25 <p>Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better.</p>
26 <cite>― Edsger W. Dijkstra</cite>
27</blockquote>
28
29
30## Getting started
31
32There's a [whole tutorial on getting started for beginners](https://serverjs.io/tutorials/getting-started/) but the quick version is to first install `server` as a dependency:
33
34```bash
35npm install server
36```
37
38> Server requires **Node.js 7.6.0** or newer. **Node.js 8.x.y** LTS is recommended.
39
40Then you can create a file called `index.js` with this code:
41
42```js
43// Include the server in your file
44const server = require('server');
45const { get, post } = server.router;
46
47// Handle requests to the url "/" ( http://localhost:3000/ )
48server([
49 get('/', ctx => 'Hello world!')
50]);
51```
52
53Execute this in the terminal to get the server started:
54
55```bash
56node .
57```
58
59And finally, open your browser on [localhost:3000](http://localhost:3000/) and you should see 'Hello world!' on your browser.
60
61
62
63## Documentation
64
65The library is documented here:
66
67<strong><a class="button" href="https://serverjs.io/documentation/">Full Documentation</a></strong>
68
69> [**Subscribe here**](http://eepurl.com/cGRggH) to receive tutorials when released. Tutorials are *good for learning* while the documentation is good for reference/quick use *once you know the basics*.
70
71You can also download the repository and try the examples by browsing to them and `node .` inside each of them in `/examples`.
72
73
74
75## Use cases
76
77The package `server` is great for many situations. Let's see some of them:
78
79
80### Small to medium projects
81
82Everything works out of the box, you get great support for most features and you can easily tap into Express' middleware ecosystem. What's not to love?
83
84Some of the included features: body and file parsers, cookies, sessions, websockets, Redis, gzip, favicon, csrf, SSL, etc. They just work so you will save a headache or two and can focus on your actual project. Get a simple form going:
85
86```js
87const server = require('server');
88const { get, post } = server.router;
89const { render, redirect } = server.reply;
90
91server(
92 get('/', () => render('index.pug')),
93 post('/', ctx => {
94 console.log(ctx.data);
95 return redirect('/');
96 })
97);
98```
99
100
101
102### API design
103
104From the flexibility and expressivity of the bundle, designing APIs is a breeze:
105
106```js
107// books/router.js
108const { get, post, put, del } = require('server/router');
109const ctrl = require('./controller');
110
111module.exports = [
112 get('/book', ctrl.list),
113 get('/book/:id', ctrl.item),
114 post('/book', ctrl.create),
115 put('/book/:id', ctrl.update),
116 del('/book/:id', ctrl.delete)
117];
118```
119
120
121
122### Real time
123
124Websockets were never this easy to use! With socket.io on the front-end, you can simply do this in the back-end to handle those events:
125
126```js
127// chat/router.js
128const { socket } = require('server/router');
129const ctrl = require('./controller');
130
131module.exports = [
132 socket('connect', ctrl.join),
133 socket('message', ctrl.message),
134 socket('disconnect', ctrl.leave)
135];
136```
137
138
139
140## Author & support
141
142This package was created by [Francisco Presencia](http://francisco.io/) but hopefully developed and maintained by many others. See the [the list of contributors here](https://github.com/franciscop/server/graphs/contributors).
143
144I love using my work and I'm available for contractor work. Freelancing helps maintain `server` and [my other open source projects](https://github.com/franciscop/) up to date! I am also on [Codementor](https://www.codementor.io/franciscop) so if you want to learn more Javascript/Node.js/etc contact me there.
145
146You can also [sponsor the project](https://serverjs.io/sponsor), get your logo in here and some other perks with tons of ♥