The node.js powered server side framework express.js is a pretty when it comes to making full stack web applications with node.js. It is part of the MEAN stack, and is also a major component of many other projects like sails, and keystone. In any case express is worthy of a series of posts on it for starting with the typical getting started post, so lets get this one out of the way so we can get into making some interesting stuff.
So far when I work with express I just start a new npm project, and install express.js as one of the dependencies like with any other node.js project.
An express.js hello world might involve just getting e reference to what is exported from the express module like with any npm package, then calling the function that is given when doing so which will result in an instance of an express app object.
Once you have the app object you would want to set up one or more paths that will respond to get requests, and use send response method to respond with the sting ‘hello world’.
It is common to call the main script that will start the project app.js, and to place this file in the root name space of the project. In all my demos I tent to stay true to that practice.
The next step from your first express.js hello world project might be to get into how to serve up static files, render templates with a rendering engine like ejs, or pug, or learn more about routing. Once you have those things down you might be in a good place to start experimenting with different stacks, including database options if the project has a need for one.
You might want to check out my post on staic file hosting in express, but it is not to hard. Just have a public folder in the root of the project, place static assets in there including an index.html file, and add this to the app.js file.
Of course there are options that can be passed to the express.static method via a second argument, and sometimes you might want to bind to a path other than rought but thats the basic idea.
For example I can make a file at /routes/foo.js that responds to any get request with bar.
And then I can then bind it to a certain path, and add it to my main app.js file using app.use.
As an application grows more complex I can pull paths into these seperate files that make used of routers, export thous routers, and then make use of theme in my main script using app.use.
Thats it for my getting started post on express.js, as my content and demos on this subject grow it can be expected that this post will be revised.