Basic Development Server
Current Version : 2.0.0
This project aims at providing a very (very (very)) basic development server with
minimal configuration and maximum ease of use in order to make the setup of a
simple develpoment environment deadly fast and easy.
The project is built with the Express framework and is
intended to be modular and adaptable.
Installation
To use this dev server you need to have Node.js
installed on your machine.
Then clone this repository in the directory of your choice and launch : npm install
in that directory.
To run the server launch : npm start
.
The server should now be up on localhost and listening on port 8081
(default).
You can stop the server by typing Ctrl+C
.
Usage
This little dev server was first developed in order to mock basic REST APIs.
The idea is to provide a simple way to build a fake API that will be used to
test another app that use this API. By extension it allows you to prototype
your API and test some aspects of its behavior before you start a project.
Create an API
To create a new API, just create a new JS file in the /src/http
directory.
This file will contain your API implemention, it must exports a function that
take a server
parameter and that defines your API as in an Express app.
Below is an example for default-api.js
:
module.exports = (server) => {
server.get('/api', (req, res, next) => {
res.send('Default API');
});
server.get('/', (req, res, next) => {
res.send('<h1>Hello World !</h1>');
});
server.get('/:name', (req, res, next) => {
res.send(`<h1>Hello ${req.params.name} !</h1>`);
});
};
Note : the basic-dev-server
use the nodemon
server
to provide an even easier development experience, once the server is started you
can start editing your API code and it will be automatically reloaded when saving,
so that you can immediately test it (e.g in your favorite browser).
Create a Configuration
You control the port on which the server is listening as well as the API that is
loaded by providing a configuration file.
Here is an example of such a configuration (see default-conf.js) :
module.exports = {
http: {
port: 8081,
api: 'default-api'
}
};
http.port
: port on which the server will be listening for incoming requestshttp.api
: list of APIs to load
For example if I just defined my new awesome API in a file named my-awesome-api.js
and put it in the /src/http
directory, then I would load this API by adding the
string 'my-awesome-api'
to the http.api
array.
Use a Configuration
Once you created your configuration you can tell the server to use it at startup
with the following command : npm start <my-config>
.
Here <my-config>
is the name of the configuration file.
The server will look into the conf/
directory for the specified config file.
Note :
- if no configuration file is specified the default configuration defined in
conf/default-conf.js is loaded
- only one configuration can be loaded at a time
Contributing
This basic-dev-server
is published under the GPLv3 so feel free to use, copy,
redistribute and/or improve it as you like !
(see LICENSE for more details).