Creating the Node.js server

The first step in developing the notes web application is coding the HTTP server that will serve the clients' requests, so we're going to start with the app.js file.

The HTTP server relies on the Express.js module, so we first need to import the module and all other modules that will be used in the app.

Take a look at the following code:

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io')(server),
path = require('path'),
bodyParser = require('body-parser'),
model = require('./models/rethinkdb');

These seven lines actually do quite a bit of work! Let's go through them line by line.

The first line imports the Express.js module and makes it accessible through the express variable so that we can use it in the script.

Line number two creates a new instance of an Express.js app, while line three imports the http module and uses the app variable to create an instance of an HTTP server.

Line number four imports the Socket.io module and initializes it by passing the HTTP server as a parameter.

The following two lines import two utility modules that will be used through the chapter, while the last line imports the RethinkDB model file and makes it accessible through the model variable so that we can use it in the script.

Before we start the HTTP server, we must configure a few options of the Express.js framework:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.set("model", model);
model.setup();

These lines instruct the Express.js framework that we're going to parse JSON and URL-encoded data and specify the path of two folders: the folder that contains the app's views and the folder that contains static files that should be served by the HTTP server.

Finally, we specify the view engine we're going to use (EJS), save a reference to the model so that we can access it from other scripts, and call the setup() function that initializes the model. We'll look at this in more detail further on in the chapter. For now, you just need to know that this function creates the necessary tables in the database that is going to contain our data.

Now that we've successfully configured our Express.js web app, there are just two things we need to do: set up the router and start the server:

var routes = require('./routes/index')(app);

The previous line loads the index.js file from the routes folder and initializes it. What this means is that when the HTTP server receives a request, it will pass the request to the routes file so that we can handle each request differently.

Finally, we can start the HTTP server:

server.listen(8000, function() {
console.log("Server up and listening on port 8000");
});

As you can imagine, these lines instruct the HTTP server to start listening for requests on port 8000 and print a log message.

The final app.js script will look as this:

#!/usr/bin/node
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io')(server),
path = require('path'),
bodyParser = require('body-parser'),
model = require('./models/rethinkdb');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.set("model", model);
model.setup();

var routes = require('./routes/index')(app);
server.listen(8000, function() {
    console.log("Server up and listening on port 8000");
});

At this point, our web application is up and running and can receive HTTP requests from the browser, passing them on to the router. The next step is to create the router for our application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.171.90