A socket wrapper packer for KOS NG IT Konsult
npm install @kos-ng-it-konsult/socket$ npm i --save @kos-ng-it-konsult/socket
"use strict";const express = require('express'); // run npm install --save express
const bodyparser = require('body-parser'); // run npm install --save body-parser
const http = require('http');
const socket = require('@kos-ng-it-konsult/socket');
const app = express();
app.use(bodyparser.urlencoded({ extended: true, limit: '100mb' }));
app.use(bodyparser.json({ limit: '100mb' }));
function endRequest () {
return (request, response, next) => {
return response.status(200).send({
status: 'success',
message: 'thank you'
});
};
}
app.route('/test')
.get(endRequest())
.post(endRequest()); // Authenticate user and then authorize user action
app.route('/test/:user_id')
.get(endRequest()); // Set strict authentication to false
const server = http.Server(app);
socket.connect(server);
server.listen((process.env.PORT || 3005), (error) => {
if (!error) {
console.log(
server is listening on port ${ (process.env.PORT || 3005) });
} else {
console.log(Error starting server: ${ error.toString() });
}
});app.use((error, request, response, next) => {
response.status((error.statusCode || 500)).send({
status: 'error',
message: (error.message || 'Internal server error, please try again')
});
});
`
Running the Example
To run the example, first install express and body-parser using\
$ npm install --save express\
$ npm install --save body-parser\Then run the sample command\
$ node ./example/sample.jsRunning Tests
Run\
$ npm test`