Setting up a static file server with hapi.
So now that I have got the simple hello world project down with hapi, it is time to find out how to make a simple static file server with hapi. This should not be to hard, so lets get it out of the way so I can move on to the real cool stuff.
Getting started
So basically its the same setup process as the hello world project, but now I am going to be installing one plug-in for hapi called inert
So I would start by making a new project folder and do the usual deal when starting a new node project with npm init, to get a package.json file. Then install hapi, and the inert plugin-in.
|
|
So then his will also be a quick intro on how to get stared with plug-ins in hapi also, although I will not get into detail with that here.
The server.js file
I will want to write an index.js, or server.js file in the root of the project that is what will be called to start the server. I usually go with server.js, the code of the file looks like this:
|
|
Server.register is what to use to go about doing something with a plugin in hapi, I could pass inert some options, but for now I just want to get going.
I could also set it up to serve just a single file rather than the whole public folder, but generally this is all I need it to do, and I can expand from here if needed.
So in this server.js file when I start it with node in will serve up a static collection of files that I have in the public folder.
The public folder
The public folder can be any kind of static collection of html, css, images, and js files structured in whatever way works for me. Sense this is a post in which things are just about getting started rather than a certain front end technology to use, the public folder can just contain a simple index.html file that just looks like this:
|
|
Starting the server
Once everything is set up it is then just a questing of cding to the root name space of the project and starting the server.js file in the command line.
|
|
Then just go to localhost:3000 in my browser, and I am ready to move forward with things.
Conclusion
Setting up this kind of project is important when it comes to testing out new front end projects. For example I am starting with this kind of project to host a whole bunch of phaser demos that I am working on.
When it comes to making an actual website I may or may not take this approach. When it comes to static site generators I am using hexo, and for full featured Content Management Systems I have my eyes in keystone.
Thats it for now, Happy coding.