1. At first, we create the repository, i'm using bitbucket and mercurial 2. We clone the repository locally educacion@educacion:~/src/resthapi$ hg clone https://bitbucket.org/maximilianou/rest_echo_hapi/ 3. List this folder to see the files. educacion@educacion:~/src/resthapi$ ls rest_echo_hapi 4. Change Dir, inside the path where you will have the code. educacion@educacion:~/src/resthapi$ cd rest_echo_hapi/ educacion@educacion:~/src/resthapi/rest_echo_hapi$ ls -a . .. .hg LICENSE.txt README.md 5. Create the package.json educacion@educacion:~/src/resthapi/rest_echo_hapi$ npm init -y 6. let see the repository files status sync educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status ? package.json 7. Add new files educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg add . 8. Verify the locally status change. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status A package.json 9. Commit changes in the local repository. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg commit -m 'initial commit' -u maximilianou@gmail.com 10. Upload Changes to the remote repository ( origin master ) educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg push 11. Let's install Hapijs educacion@educacion:~/src/resthapi/rest_echo_hapi$ npm install hapi --save 12. Verify the changes in package.json educacion@educacion:~/src/resthapi/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": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "hapi": "^17.8.1" } } 13. Add to .hgignore what does'nt have to be sync to the central ( remote ) repository. educacion@educacion:~/src/resthapi/rest_echo_hapi$ vi .hgignore 14. Let's see the content of my hgignore now. educacion@educacion:~/src/resthapi/rest_echo_hapi$ cat .hgignore # Add any directories, files, or patterns you don't want to be tracked by version control # Project # # System # .DS_Store # Mercurial # .orig # Node # node_modules/ # Grunt build # dist/ # Bower # bower_components/ public/system/lib .bower-cache/ .bower-registry/ .bower-tmp/ .tmp/ # IDE # nbproject/ .idea/ atlassian-ide-plugin.xml 15. Add configuration files for this instance of reposisory. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg add . adding .hgignore adding package-lock.json educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status M package.json A .hgignore A package-lock.json 16. Commit this repository sanity change. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg commit -m 'repository sanity' -u maximilianou@gmail.com 17. Upload to the central repository educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg push 18. Now, we can create a directory structure, just to beginning with the right foot, then walk. educacion@educacion:~/src/resthapi/rest_echo_hapi$ mkdir server 19. Create our first javascript server file, or program. educacion@educacion:~/src/resthapi/rest_echo_hapi$ touch server/index.js 20. Here we create the server in port 3000, localhost. Object Oriented Programming. educacion@educacion:~/src/resthapi/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.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); } }; }; const firstEchoServer = new EchoServerHapi({ port: 3000, host: 'localhost', app: {} }); firstEchoServer.init(); 21. It is running. educacion@educacion:~/src/resthapi/rest_echo_hapi$ node server/index.js Here we have the echo Server Hapi running:: http://localhost:3000 22. And.. the route is not specified. educacion@educacion:~$ curl http://localhost:3000/ {"statusCode":404,"error":"Not Found","message":"Not Found"} 23. Adding some route. Simple way at first, just a method routing() in the class. educacion@educacion:~/src/resthapi/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'; } }); } } const firstEchoServer = new EchoServerHapi({ port: 3000, host: 'localhost', app: {} }); firstEchoServer.init(); 24. running the server, from the command line. educacion@educacion:~/src/resthapi/rest_echo_hapi$ node server/index.js Here we have the echo Server Hapi running:: http://localhost:3000 25. Checking the path / educacion@educacion:~/src/resthapi/rest_echo_hapi$ curl http://localhost:3000/ Hello, Javascript Developer! 26. Checking the path /Mafalda, just an Example Case!! educacion@educacion:~/src/resthapi/rest_echo_hapi$ curl http://localhost:3000/Mafalda Hello, Mafalda! 27. Upload changes to the central repository. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status M server/index.js educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg commit -u maximilianou@gmail.com -m 'Hello URL routing()' educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg push 28. Now, adding GET, POST, PUT to our near rest echo. 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'; } }); } } const firstEchoServer = new EchoServerHapi({ port: 3000, host: 'localhost', app: {} }); firstEchoServer.init(); 29. Now, checking the post and put, with --data, body of http for POST and PUT, Reference: https://tools.ietf.org/html/rfc2616 educacion@educacion:~/src/resthapi/rest_echo_hapi$ curl --header "Content-Type: application/json" --request POST --data '{"clave":"valor"}' http://localhost:3000/msg POSTing:: {"clave":"valor"} educacion@educacion:~/src/resthapi/rest_echo_hapi$ curl --header "Content-Type: application/json" --request PUT --data '{"clave":"valor"}' http://localhost:3000/msg PUTing:: {"clave":"valor"} 30. Commiting changes to the central repository. educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg status M server/index.js educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg commit -u maximilianou@gmail.com -m 'post, put method echo' educacion@educacion:~/src/resthapi/rest_echo_hapi$ hg push 31. Compliting echo rest, get, post, put, and DELETE here 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'; } }); } } const firstEchoServer = new EchoServerHapi({ port: 3000, host: 'localhost', app: {} }); firstEchoServer.init(); 32. Checking HTTP DELETE, data into the url, like http get. educacion@educacion:~$ curl --request DELETE http://localhost:3000/Pedrito Bye, Pedrito!
Think the best of others
Friday, December 21, 2018
rest hapi echo
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment