1. The javascript echo parameter server hapi
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ cat server/index.js
const Hapi = require('hapi');
class EchoServerHapi{
constructor(args){
this.server = Hapi.server(args);
}
async init(){
try {
await this.routing();
await this.server.start();
console.log(`Here we have the echo Server Hapi running:: ${this.server.info.uri}`);
} catch (error) {
console.log('Error while trying to run echo Server Hapi:: ' + error.message);
}
}
async routing(){
this.server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello, Javascript Developer! ! !\n';
}
});
this.server.route({
method: 'GET',
path: '/{name}',
handler: (request, h) => {
return 'Hello, ' + encodeURIComponent(request.params.name) + '!\n';
}
});
this.server.route({
method: 'POST',
path: '/msg',
handler: (request, h) => {
return "POSTing:: "+JSON.stringify(request.payload) + '\n';
}
});
this.server.route({
method: 'PUT',
path: '/msg',
handler: (request, h) => {
return "PUTing:: "+JSON.stringify(request.payload) + '\n';
}
});
this.server.route({
method: 'DELETE',
path: '/{name}',
handler: (request, h) => {
return 'Bye, ' + encodeURIComponent(request.params.name) + '!\n';
}
});
}
}
/* LOOK THIS COMMENT, NO localhost, just 0.0.0.0 */
/*const firstEchoServer = new EchoServerHapi({
port: 3001,
host: 'localhost',
app: {}
});*/
const firstEchoServer = new EchoServerHapi({
port: 3001,
host: '0.0.0.0',
app: {}
});
firstEchoServer.init();
2. The node package.json done with npm
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ cat package.json
{
"name": "rest_echo_hapi",
"version": "1.0.0",
"description": "This README would normally document whatever steps are necessary to get your application up and running.",
"main": "server/index.js",
"scripts": {
"start": "node server/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "maximilianou@gmail.com",
"license": "MIT",
"dependencies": {
"hapi": "^17.8.1",
"isemail": "^3.2.0"
}
}
3. The Dockerfile to run and modify the javascript program
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ cat Dockerfile
FROM node
WORKDIR /code
RUN npm set prefix /usr/local && npm install -g nodemon
COPY package.json /code/package.json
RUN npm set prefix /usr/local && npm install && npm ls
RUN mv /code/node_modules /node_modules
COPY . /code
#EXPOSE 3000 # expose but no publish, except useing docker -P
CMD ["npm", "start"]
4. The docker compose to run the composition of service docker having volume shared path
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ cat docker-compose.yml
version: "2"
services:
web:
build: .
command: nodemon -L --inspect=0.0.0.0:5858
volumes:
- .:/code
ports:
- "3001:3001"
- "5858:5858"
5. Starting from command line with npm, from package.json
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ npm start
> rest_echo_hapi@1.0.0 start /home/educacion/src/resthapi/v2/rest_echo_hapi
> node server/index.js
Here we have the echo Server Hapi running:: http://0.0.0.0:3001
6. Starting from docker compose
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ docker-compose up
Starting restechohapi_web_1
Attaching to restechohapi_web_1
web_1 | [nodemon] 1.18.7
web_1 | [nodemon] to restart at any time, enter `rs`
web_1 | [nodemon] watching: *.*
web_1 | [nodemon] starting `node --inspect=0.0.0.0:5858 server/index.js`
web_1 | Debugger listening on ws://0.0.0.0:5858/e6293311-3e2c-4f77-af38-d087a52d30c3
web_1 | For help, see: https://nodejs.org/en/docs/inspector
web_1 | Here we have the echo Server Hapi running:: http://0.0.0.0:3001
7. Checking GET, POST, PUT and DELETE echoing the parameter in each case.
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl http://0.0.0.0:3001
Hello, Javascript Developer! ! !
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl http://127.0.0.1:3001
Hello, Javascript Developer! ! !
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl http://localhost:3001
Hello, Javascript Developer! ! !
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl http://localhost:3001/Hijitus
Hello, Hijitus!
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl --request DELETE http://localhost:3001/Larguirucho
Bye, Larguirucho!
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl --header "Content-Type: application/json" --request POST --data '{"name":"Pucho"}' http://localhost:3001/msg
POSTing:: {"name":"Pucho"}
educacion@educacion:~/src/resthapi/v2/rest_echo_hapi$ curl --header "Content-Type: application/json" --request PUT --data '{"name":"Profesor Neurus"}' http://localhost:3001/msg
PUTing:: {"name":"Profesor Neurus"}
Think the best of others
Wednesday, December 26, 2018
docker rest api hapijs nodejs vscode realtime development
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment