node.js framework - express.js

in #express7 years ago (edited)

node.js framework - express.js

http://expressjs.com/

  1. Framework backgrounds:

The initial commit for Express was made on June 26, 2009 by TJ Holowaychuk and 660 commits later version 0.0.1 was released on January 2, 2010. The two main contributors at that time were TJ and Ciaron Jessup. At the time of the first release the framework was described as per the readme.md on github
Insanely fast (and small) server-side JavaScript web development framework built on node.js and V8 JavaScript engine.
Fast forward almost 5 years and 4,925 commits, now 4.10.1 is the latest version maintained by StrongLoop as TJ is now concentrating in the Go-Lang community.

Creating a server:
var express = require('express');
var app = express();

var server = app.listen(3000, function() {
console.log('Express is listening to http://localhost:3000');
});

This is probably pretty natural to all node developers. We require express and then instantiate it by assigning it to the variable app. Then instantiate a server to listen to a port, port 3000. The app.listen() is actually just a wrapper around node's http.createServer().

  1. Routes:
    ar express = require('express');
    var app = express();

app.get('/', function(req, res) {
res.send('Hello world');
});

var server = app.listen(3000, function() {
console.log('Express is listening to http://localhost:3000');
});

We are using the get() method to catch the incoming request of "GET /" and then invoke a callback function that handles two parameters req and res. For this example we are only utilizing res to return back to the page a string using res.send(). Express has a variety of built in methods that are used to handle the routing functionality. The following are some of the more commonly used methods that are supported by Express (but not all of the methods): get, post, put, head, delete..

  1. REST API:
    var express = require('express');
    var app = express();
    var router = express.Router();

// REST API
router.route('/items')
.get(function(req, res, next) {
res.send('Get');
})
.post(function(req, res, next) {
res.send('Post');
});

router.route('/items/:id')
.get(function(req, res, next) {
res.send('Get id: ' + req.params.id);
})
.put(function(req, res, next) {
res.send('Put id: ' + req.params.id);
})
.delete(function(req, res, next) {
res.send('Delete id: ' + req.params.id);
});

app.use('/api', router);

// index
app.get('/', function(req, res) {
res.send('Hello world');
});

var server = app.listen(3000, function() {
console.log('Express is listening to http://localhost:3000');
});

So we added our REST API to our existing Hello World application. Express offers a little shorthand for handling routes. This is Express 4.x syntax but it is essentially the same in Express 3.x except you don't need the express.Router() and you will not be able to use the line app.use('/api', router). Instead you will replace the router.route()'s with app.route() while prepending the existing verb with /api. This is a nice approach because it reduces the chance of developer errors and minimizes the places to change the HTTP method verbs.

  1. the Good:
    Express has the biggest community not only out of the three frameworks compared here but out of all the web application frameworks for Node.js. It is the most matured framework out of the three, with almost 5 years of development behind it and now has StrongLoop taking control of the repository. It offers a simple way to get a server up and running and promotes code reuse with it's built in router.

  2. the Bad:
    There is a lot of manual tedious tasks involved in Express. There is no built in error handling, it is easy to get lost in all of the middleware that could be added to solve a solution, and there are many ways to do one thing. Express describes itself as being opinionated, this could be good or bad but for beginning developers who must likely chose Express, this is a bad thing. Express also has a larger foot print compared to the other frameworks.

Sort:  

잘못올렸는데 업봇해주셔서 감사합니다. 팔로우하고갑니다

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.031
BTC 60795.60
ETH 2627.31
USDT 1.00
SBD 2.58