https://github.com/jakecyr/slim-node-server
Simple and slim Node.js HTTP server
https://github.com/jakecyr/slim-node-server
http-server nodejs nodejs-framework nodejs-server slim-framework
Last synced: 10 months ago
JSON representation
Simple and slim Node.js HTTP server
- Host: GitHub
- URL: https://github.com/jakecyr/slim-node-server
- Owner: jakecyr
- Created: 2019-12-17T14:05:49.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-10T01:25:08.000Z (almost 6 years ago)
- Last Synced: 2025-02-17T03:02:11.910Z (10 months ago)
- Topics: http-server, nodejs, nodejs-framework, nodejs-server, slim-framework
- Language: TypeScript
- Homepage:
- Size: 90.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Slim Node.js HTTP Server
[](https://travis-ci.com/jakecyr/slim-node-server)
## Installation
* Install the npm package `npm install slim-node-server`
* Import the module to your project (see example below)
## Usage
After installing installing the framework, create a new instance of `Slim` and start creating your server:
```javascript
// import slim framework
const Slim = require('slim-node-server');
// create new slim server with logging
let app = new Slim(true);
app
.get('/', (req, res) => {
res.end('nothing here yet');
})
.get('/some-json', (req, res) => {
res.json({ task: 'Task 1' });
})
.listen(8080, '0.0.0.0', () => {
console.log('Listening');
});
```
### Middleware
* All request types support middleware functions
* The last function must end the response
* All route handler functions are executed in the order they are specified
Example:
```javascript
function log(req, res, next) {
console.log('LOG VISIT', req.url);
next();
}
// add middleware to log page url visited to server console
app.get('/', log, (req, res) => {
res.end('Visit has been logged');
});
```
### Query Parameters
Parse query parameters as needed using the `request` object in a handler function. Example:
```javascript
app.get('/echo-name', (req, res) => {
const queryParams = req.query();
res.end(queryParams.name);
})
```
### Body Parsing
Parse a payload body as needed using the `request` object in a handler function. Example:
```javascript
app.post('/', async (req, res) => {
// wait for all payload data to parse
const payload = await req.body();
// echo back the payload to the client
res.json(payload);
})
```
### Router
Routes can be imported from other files to reduce file size. Example:
```javascript
app
.addRoutes('/api', require('./routes/'))
.listen(8080, '0.0.0.0', () => console.log('Server listening'));
```