UNPKG

1.1 kBJavaScriptView Raw
1'use strict';
2
3var a127 = require('a127-magic');
4var express = require('express');
5var app = express();
6
7module.exports = app; // for testing
8
9// initialize a127 framework
10a127.init(function(config) {
11
12 // include a127 middleware
13 app.use(a127.middleware(config));
14
15 // error handler to emit errors as a json string
16 app.use(function(err, req, res, next) {
17 if (typeof err !== 'object') {
18 // If the object is not an Error, create a representation that appears to be
19 err = {
20 message: String(err) // Coerce to string
21 };
22 } else {
23 // Ensure that err.message is enumerable (It is not by default)
24 Object.defineProperty(err, 'message', { enumerable: true });
25 }
26
27 // Return a JSON representation of #/definitions/ErrorResponse
28 res.set('Content-Type', 'application/json');
29 res.end(JSON.stringify(err));
30 });
31
32 var ip = process.env.IP || 'localhost';
33 var port = process.env.PORT || 10010;
34 // begin listening for client requests
35 app.listen(port, ip);
36
37 console.log('try this:\ncurl http://' + ip + ':' + port + '/hello?name=Scott');
38});