Right I'm just working my way through the tutorials and let me tell you, this gives you an appreciation for all the PHP functions and code igniter functions that we take for granted.
Even serving an image file took some time to figure out.
So far, I've successfully created a server, created routes for a html file that serves all content within. Parsed POST requests, still need to figure a way to send it back to the view, I think I might need handlebars?
Still need to install bcrypt, for password hashing, and the mysql npm to connect and talk to the database, joi for form validation, and either formidable or multr for file uploading and nodemailer for sending emails!
To kill the node process I need to use killall -9 node
Code:
'use strict';
const Hapi = require('hapi');
const server = Hapi.server({
port: 3000,
host: 'localhost'
});
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello, world!';
}
});
server.route({
method: 'GET',
path: '/{name}',
handler: (request, h) => {
return 'Hello, ' + encodeURIComponent(request.params.name) + '!';
}
});
const init = async () => {
await server.register(require('inert'));
await server.register(require('vision'));
//serve any images
server.route({
method: 'GET',
path: '/public/img/{param*}',
handler: {
directory: {
path: 'public/img'
}
}
});
//post test
server.route({
method: 'POST',
path: '/formsent',
handler: (request, h) => {
//get the post of name
//const payload = request.payload
//return payload.name
//pass this back something back into the view
//should I use handlebars?
return h.redirect('hello',{title:'my home page'})
}
});
server.route({
method: 'GET',
path: '/hello',
handler: (request, h) => {
return h.file('./public/hello.html');
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();