Getting started with Express 10-25-2016, 01:31 PM
#1
Express is a very minimalist, extensible, and plugin-supportive web framework for node.js (and things that compile to it like CoffeeScript, by extension). It's super easy to use in any way that you need it. As a beginning tutorial, I'll show how to make a simple web API with it.
Requirements
Before we start, make sure you have NodeJS and NPM installed (they usually come together). If you don't already, you can download them here.
Setting up the package
If you haven't used NPM before, it's essentially a dependency manager and sort of an SVC solution. All we need it for now, though, is the dependencies. First, copy the code below into a file called package.json, navigate to the directory in your terminal, and run "npm install". If you see a bunch of tree structures, it worked.
Hello $name
As is customary for every new programming tool, we'll setup the app and do a quick (but modified) hello world to demonstrate how Express works on a basic level.
Now let's break down what's going on here.
The first two lines are just "requiring" the Express and Morgan packages that we installed through NPM. This is the Node equivalent of a Python, Java, or C# "import" or a C/C++ "#include", but the module (exported package contents) are assigned to a variable.
Here, we create the Express application and assign it to the "app" variable. Then, we tell the app to use Morgan (which we "require"d as logger) in the development setting.
This is the "router" part of our app; it tells Node what to give the user based on their HTTP method (get, post, put, delete, etc...) and path. Our route here is /hello, but it takes a parameter: name. This name parameter can be accessed through the "params" object of the request.
After constructing our message to the user, we send it off to them using the result.send method.
This part tells the app what port to listen on (the address defaults to 127.0.0.1). A nice thing about node is that you can access any environment variables through process.env and if they don't exist, you get "undefined" (which evaluates to false and gives us 3000, in this case).
You can run the app by saving it to a file (which I usually call app.js) and running it with node:
Then simply point your browser to 127.0.0.1:3000/hello/YourName (or a different port if you have that set) and watch!
Componentizing the router
For this part, think large applications. For example, I built the beginnings of a social network for my hackathon with Express. Imagine if all the routes - homepage, users, login, registry, groups, posts, etc... - were all defined on one page. That would suck. To avoid this, Express has an independent router class that you can include in your app.
Create a "routes" directory and in it, create a file called hello.js. Cut and paste the router section of the last example and remove the /hello so it looks like this:
This won't yet work, so we need to change a few things. First, at the top of the file, require Express again, but after it assign a "router" variable to express.Router(), like so:
Now that we have the Express parts that we need, replace "app" in the routing function with "router", then set the module exports (see, it was relevant) to the router variable.
Now back in app.js, we can tell our Express app to "use" the router we just defined for a specific path by requiring it, like below:
(sidenote: you can start the interactive node shell to see what's going on with the required route. Just enter the "require('./routes/hello')" bit and see what it returns)
Once again, run the app and point your browser to the address to see if it works.
When I have an hour or two to spare, I'll expand on Express by going over template engines. In preparation for that, I recommend checking out fuck off as a service, which is hilarious but a practical example of combining routing techniques and template engines. Also, if you want to see the full code, I created a gist just for that.
Requirements
Before we start, make sure you have NodeJS and NPM installed (they usually come together). If you don't already, you can download them here.
Setting up the package
If you haven't used NPM before, it's essentially a dependency manager and sort of an SVC solution. All we need it for now, though, is the dependencies. First, copy the code below into a file called package.json, navigate to the directory in your terminal, and run "npm install". If you see a bunch of tree structures, it worked.
Code:
{
"name": "express-demo",
"version": "0.1.0",
"dependencies": {
"express": "~4.13.4",
"morgan": "~1.7.0"
},
"private": true
}Hello $name
As is customary for every new programming tool, we'll setup the app and do a quick (but modified) hello world to demonstrate how Express works on a basic level.
Code:
var express=require('express'),
logger=require('morgan');
var app=express();
app.use(logger('dev'));
app.get('/hello/:name',function(req,res){
res.send('Hello, '+req.params.name+'!');
});
app.listen(process.env.PORT||3000);Now let's break down what's going on here.
Code:
var express=require('express'),
logger=require('morgan');Code:
var app=express();
app.use(logger('dev'));Code:
app.get('/hello/:name',function(request,result){
result.send('Hello, '+request.params.name+'!');
});After constructing our message to the user, we send it off to them using the result.send method.
Code:
app.listen(process.env.PORT||3000);You can run the app by saving it to a file (which I usually call app.js) and running it with node:
Code:
$ node app.jsComponentizing the router
For this part, think large applications. For example, I built the beginnings of a social network for my hackathon with Express. Imagine if all the routes - homepage, users, login, registry, groups, posts, etc... - were all defined on one page. That would suck. To avoid this, Express has an independent router class that you can include in your app.
Create a "routes" directory and in it, create a file called hello.js. Cut and paste the router section of the last example and remove the /hello so it looks like this:
Code:
app.get('/:name',function(req,res){
res.send('Hello, '+req.params.name+'!');
});Code:
var express=require('express'),
router=express.Router();Now that we have the Express parts that we need, replace "app" in the routing function with "router", then set the module exports (see, it was relevant) to the router variable.
Code:
var express=require('express'),
router=express.Router();
router.get('/:name',function(req,res){
res.send('Hello, '+req.params.name+'!');
});
module.exports=router;Now back in app.js, we can tell our Express app to "use" the router we just defined for a specific path by requiring it, like below:
Code:
app.use('/hello',require('./routes/hello'));Once again, run the app and point your browser to the address to see if it works.
When I have an hour or two to spare, I'll expand on Express by going over template engines. In preparation for that, I recommend checking out fuck off as a service, which is hilarious but a practical example of combining routing techniques and template engines. Also, if you want to see the full code, I created a gist just for that.
(This post was last modified: 10-25-2016, 01:40 PM by Inori.)
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.
















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)









![[Image: 7ajmN5P.jpg]](https://i.imgur.com/7ajmN5P.jpg)