🖥 Simple and powerful server for Node.js
Branch: master
Clone or download
Latest commit 1f852c2 Jun 14, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update Ask_question.md May 14, 2018
docs Updated LTS version Jun 14, 2018
error Improved testing and added better internal error handling Dec 24, 2017
examples Simplified Mar 20, 2018
plugins Added session to the sockets. Fixed test and docs for jsonp Jan 1, 2018
reply Added session to the sockets. Fixed test and docs for jsonp Jan 1, 2018
router Updated documentation and added test for matching the error by path. … Jan 24, 2018
src Normalized folders Jan 9, 2018
test Updated test Mar 22, 2018
.eslintrc.json Added eslint and migrated to the Reply methods. Fixes #11. New alpha … May 28, 2017
.gitignore More cleanup and refactoring. Release 49 to test in my projects Jun 2, 2017
.npmignore Fixed all tests and prepared for Beta 2 Aug 27, 2017
Contributing.md Improved contributing, added plugin log and added sponsors Mar 22, 2017
Gruntfile.js Improved documentation sections by making it automatical Jan 1, 2018
LICENSE Reinitialized it to make git smaller Mar 10, 2017
README.md Updated LTS version Jun 14, 2018
api.js Added initial specification for the stable release Jun 5, 2017
appveyor.yml Fixed windows tests? Nov 1, 2017
changelog.md Added the security incident report link and changelog Feb 16, 2018
circle.yml Large refactoring Jun 2, 2017
package.json Fixed latest tag for npm Feb 15, 2018
roadmap.md Large refactoring Jun 2, 2017
server.js Added session to the sockets. Fixed test and docs for jsonp Jan 1, 2018

README.md

server.js for Node.js

Subscribe Downloads Status Windows Build Dependencies status

Powerful server for Node.js that just works so you can focus on your awesome project:

// Include it and extract some methods for convenience
const server = require('server');
const { get, post } = server.router;

// Launch server with options and a couple of routes
server({ port: 8080 }, [
  get('/', ctx => 'Hello world'),
  post('/', ctx => console.log(ctx.data))
]);
If you want to quickly add authentication to Node.js apps, feel free to check out Auth0's Node.js SDK and free plan at auth0.com/overview [sponsored]

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.

― Edsger W. Dijkstra

Getting started

There's a whole tutorial on getting started for beginners but the quick version is to first install server as a dependency:

npm install server

Server requires Node.js 7.6.0 or newer. Node.js 8.x.y LTS is recommended.

Then you can create a file called index.js with this code:

// Include the server in your file
const server = require('server');
const { get, post } = server.router;

// Handle requests to the url "/" ( http://localhost:3000/ )
server([
  get('/', ctx => 'Hello world!')
]);

Execute this in the terminal to get the server started:

node .

And finally, open your browser on localhost:3000 and you should see 'Hello world!' on your browser.

Documentation

The library is documented here:

Full Documentation

Subscribe here to receive tutorials when released. Tutorials are good for learning while the documentation is good for reference/quick use once you know the basics.

You can also download the repository and try the examples by browsing to them and node . inside each of them in /examples.

Use cases

The package server is great for many situations. Let's see some of them:

Small to medium projects

Everything 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?

Some 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:

const server = require('server');
const { get, post } = server.router;
const { render, redirect } = server.reply;

server(
  get('/', () => render('index.pug')),
  post('/', ctx => {
    console.log(ctx.data);
    return redirect('/');
  })
);

API design

From the flexibility and expressivity of the bundle, designing APIs is a breeze:

// books/router.js
const { get, post, put, del } = require('server/router');
const ctrl = require('./controller');

module.exports = [
  get('/book', ctrl.list),
  get('/book/:id', ctrl.item),
  post('/book', ctrl.create),
  put('/book/:id', ctrl.update),
  del('/book/:id', ctrl.delete)
];

Real time

Websockets 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:

// chat/router.js
const { socket } = require('server/router');
const ctrl = require('./controller');

module.exports = [
  socket('connect', ctrl.join),
  socket('message', ctrl.message),
  socket('disconnect', ctrl.leave)
];

Sponsors

So far we have one sponsor, Auth0! Thank you so much ❤. They are an awesome service and I'm using it at my job so I was really happy when they reached out. I fully agree with their message:

If you want to quickly add authentication to Node.js apps, feel free to check out Auth0's Node.js SDK and free plan at auth0.com/overview.

Author & support

This package was created by Francisco Presencia but hopefully developed and maintained by many others. See the the list of contributors here.

I love using my work and I'm available for contractor work. Freelancing helps maintain server and my other open source projects up to date! I am also on Codementor so if you want to learn more Javascript/Node.js/etc contact me there.

You can also sponsor the project, get your logo in here and some other perks with tons of ♥