Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

restana

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restana - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

libs/middleware-chain.js

27

index.js

@@ -17,4 +17,11 @@ const http = require("http");

const routes = {};
const middlewares = [];
let app = {
use: (middleware, context) => {
middlewares.push({
handler: middleware,
context
});
},
route: (method, path, handler, ctx = {}) => {

@@ -57,11 +64,15 @@ let key = `[${method.toUpperCase()}]${path}`;

let route = `[${req.method.toUpperCase()}]${req.path}`;
let result = wayfarer(route, req, res);
switch (result) {
case 404:
res.send(404);
break;
default:
break;
}
// calling middlewares
require('./libs/middleware-chain')(middlewares.slice(0), req, res, () => {
let result = wayfarer(route, req, res);
switch (result) {
case 404:
res.send(404);
break;
default:
break;
}
})();
},

@@ -68,0 +79,0 @@ start: (port = 3000, host) => {

@@ -1,3 +0,3 @@

module.exports.send = (req, res) => (data, code = 200, headers = {}) => {
headers['Content-Type'] = 'text/plain';
module.exports.send = (req, res) => (data = 200, code = 200, headers = {}) => {
res.setHeader('Content-Type', 'text/plain');

@@ -18,10 +18,19 @@ return new Promise(async(resolve, reject) => {

code = parseInt(data);
data = null;
} else if ('object' == typeof data) {
headers['Content-Type'] = 'application/json';
data = res.body;
}
if ('object' == typeof data) {
res.setHeader('Content-Type', 'application/json');
data = JSON.stringify(data);
}
res.writeHead(code, headers);
res.end(data, 'utf-8', (err) => {
// emit response event before send and terminate
let params = {
res,
req,
data
}
res.emit('response', params);
res.writeHead(code);
res.end(params.data, 'utf-8', (err) => {
if (err) reject(err);

@@ -28,0 +37,0 @@ resolve();

{
"name": "restana",
"version": "0.1.1",
"version": "0.2.0",
"description": "Super fast and minimalist web framework for building REST micro-services.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -12,5 +12,6 @@ # REST-Ana

const Pets = {
const PetsModel = {
// ...
};
service.get('/pets/:id', (req, res) => {

@@ -32,4 +33,11 @@ res.send(PetsModel.findOne(req.params.id));

service.patch('/pets/:id', (req, res) => {
res.send(PetsModel.update(req.params.id, JSON.stringify(req.body)));
service.patch('/pets/:id', function (req, res) {
res.send(this.update(req.params.id, JSON.stringify(req.body)));
}, PetsModel); // attaching this context
service.get('/version', function (req, res) {
res.body = { // optionally you can send the response data in the body property
version: 1.0.0
}
res.send(); // 200 is the defacult response code
});

@@ -42,2 +50,28 @@

```
Supported methods:
```js
const methods = ['get', 'delete', 'put', 'patch', 'post', 'put', 'head', 'options'];
```
Middleware usage:
```js
const service = require('restana')({});
// custom middleware to attach the X-Response-Time header to the response
service.use((req, res, next) => {
let now = new Date().getTime();
res.on('response', data => {
data.res.setHeader('X-Response-Time', new Date().getTime() - now);
});
return next();
});
// the /v1/welcome route handler
service.get('/v1/welcome', (req, res) => {
res.send('Hello World!');
});
// start the server
service.start();
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc