Scribe.js/examples/simple-server.js

96 lines
2.2 KiB
JavaScript
Raw Normal View History

import express from 'express';
import * as Scribe from '../index.js';
import * as JSON2 from '../src/libs/JSON2';
2016-01-22 08:22:11 -06:00
const port = 4005;
const options = {
"app": 'simple-server',
"id": process.pid,
"module": {
"writer/SocketIO": {
"port": 50000,
"options": {}
2016-01-22 08:22:11 -06:00
},
"router/Viewer/client": {
"background": "#131B21",
"socketPorts": [
50000
]
2016-01-22 08:22:11 -06:00
}
},
"debug": false
};
const console = Scribe.create(options);
2016-01-22 08:22:11 -06:00
console.time('serverStartup');
// default tags
console.persistent('tags', ['mocha', 'scribe']);
var through = {
2016-01-22 08:22:11 -06:00
through(data, callback){
const {req, res} = data.args[0]; // access the req, res objects
// modify data as needed
// i.e.
// add user tags
const tags = data.transient.tags || [];
tags.push('USER_ID'); // perhaps put in the user id here
data.transient.tags = tags;
callback(null, data);
}
};
// modify an existing pipeline i.e. the express
console.pipe('express', 'express-mongo-socket')
.unshift(through);
2016-01-22 08:22:11 -06:00
// express
const app = express();
2016-04-11 06:16:51 -05:00
const logger = new Scribe.Middleware.ExpressRequestLogger(console);
const viewer = new Scribe.Router.Viewer(console);
2016-01-22 08:22:11 -06:00
// express logger
app.use(logger.getMiddleware());
// viewer
app.use('/scribe', viewer.getRouter());
2016-01-22 08:22:11 -06:00
// test harness
app.get('/test', (req, res) => {
console.log({multi: "object"}, {test: 5}, {date: new Date()}).then(()=> {
res.json({test: new Array(parseInt(Math.random() * 50)).join('.')})
});
});
app.listen(port, () => {
console.log(`Listening to ${port}`);
// basic logging
function func(foo, bar) {
console.log('func')
}
console.log(JSON2.parse(JSON2.stringify([func, new Promise((req, res)=>0)])));
console.log({multi: "object"}, {test: 5}, {date: new Date()});
// override default console
console.override();
global.console.log('overriden!');
console.log(`Go to http://localhost:${port}/scribe`);
console.trace('This is a trace');
console.timeEnd('serverStartup');
});
// build native app
viewer.getNative().then(()=> console.log('Created native apps!')).catch(err => console.error(err));
setInterval(() => console.log(`Heartbeat - ${Date.now()} ... every 5 seconds`), 5000);