Giter Club home page Giter Club logo

node-server's Introduction

node-server

Express server setup for quick tests.

Here are the ~100 lines of code you need to run an express-server with CORS and helmet.

It should save you the hustle of copying and pasting those same 100 lines each time you need to try something or just want to play around.

Just specify your js-file on the command line, and you have a server running.

Installation

Usually you would install node-server as global:

sudo npm i -g windycom/node-server

Usage

node-server <js-file> [...<js-file>]

Each file (a "service") must export an (asynchronous) init()-function, either as the default (module.exports) or as a named function (module.exports.init).

The function will recieve the expressjs-app, together with an options object, and is free to do with app whatever it feels like (add middleware, set some routes etc):

module.exports = async (app, options) => {
  app.get('/', myFancyRequestHandler);
};

// or:

module.exports.init = async (app, options) => {
  app.get('/', myFancyRequestHandler);
};

Additionally, you can export a few configuration values:

module.exports.PORT = 8100;
module.exports.HOSTNAME = '127.0.0.1';
module.exports.HELMET = {
  hsts: false,
  noCache: true,
};
module.exports.CORS = {};
module.exports.DEBUG = true;

The values above are the defaults that will be used if you don't specify overrides.

Note: When you run multiple services, the configuration is taken from the first one. Values on other services will be ignored.

The options-object passed to init() has the following properties:

  • server: The http-server. Use to add e.g. websockets.
  • debug: If true, full errors will be shown (only the message otherwise).
  • port: The port used.
  • hostname: The hostname used.

When the server starts, it does the following:

  • Create an express-app via express().
  • Create a http-server via http.createServer(app).
  • Load the services you specified.
  • Set some defaults: Helmet, CORS, etc.
  • Calls each of the init-functions asynchronously: await init(app, options).
  • calls listen on server.

That's all.

License

This software is licenced under the MIT license.

node-server's People

Contributors

iunknown68 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kant

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.