Skip to content

Instantly share code, notes, and snippets.

@acauamontiel
Created July 31, 2014 19:32
Show Gist options
  • Save acauamontiel/9e8858f70310a3310f35 to your computer and use it in GitHub Desktop.
Save acauamontiel/9e8858f70310a3310f35 to your computer and use it in GitHub Desktop.
Simple server example with Hapi.js and Express.js
/////////////////////////
// Running //
// node . hapi|express //
/////////////////////////
var Example = {
hapi: function () {
var Hapi = require('hapi'),
server = new Hapi.Server('localhost', 8000);
function salute (req, rep) {
var body = 'Hello, ' + (req.params.name || 'Acauã') + '!';
if (req.query.say) {
body += ' ' + req.query.say + '!';
}
rep(body);
console.log(req, rep);
}
server.route({
method: 'GET',
path: '/',
handler: salute
});
server.route({
method: 'GET',
path: '/{name}',
handler: salute
});
server.start(function () {
console.log('Server started at', server.info.uri);
});
},
express: function () {
var express = require('express'),
app = express();
function salute (req, res) {
var body = 'Hello, ' + (req.params.name || 'Acauã') + '!';
if (req.query.say) {
body += ' ' + req.query.say + '!';
}
res.send(body);
console.log(req, res);
}
app.get('/', salute);
app.get('/:name', salute);
var server = app.listen(8000, function () {
console.log('Server started at', server.address().address + ':' + server.address().port);
});
}
}
Example[process.argv.slice(2)]();
{
"name": "Example",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "acauamontiel",
"license": "MIT",
"dependencies": {
"hapi": "~6.2.1",
"express": "~4.7.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment