![]() |
|
Tutorial Getting started with Express - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: Tutorial Getting started with Express (/Thread-Tutorial-Getting-started-with-Express) |
Getting started with Express - Inori - 10-25-2016 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. 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. RE: Getting started with Express - Oni - 10-25-2016 FOAAS - Fuck Off As A Service is hilarious.
|