Full-Stack Web Development with Vue.js and Node

(singke) #1
Building an Express Application Chapter 2

app.listen(3000, function() { console.log('listening on 3000') })

module.exports = app;

Now, run the following command:


$ node app.js

This will spin up our application server. Now, when we go to the
http://localhost:3000/ URL, we should be able to get this:


That's it. We have successfully created an Express application.


Express router


Let's move on to the Express router. As mentioned earlier in the chapter, one of the most
important aspects of Express.js is that it provides easy routing for the application. Routing


is the definition of the URL for an application. If we look at app.js, we will see a section


such as:


...
app.use('/', index);
app.use('/users', users);
...

This means that when we access a web page, and when a request is made to the home page,
the express router redirects it to a router called index. Now, look at routes/index.js,


which has the following code:


var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
Free download pdf