Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Node server with server.js


With this structure formed, we will start the creation of the server itself, which is the creation of a main JavaScript file.

The most common name used is server.js, but it is also very common to use the app.js name, especially in older versions.

Let's add this file to the root folder of the project and we will start with the basic server settings.

There are many ways to configure our server, and probably you'll find the best one for yourself. As we are still in the initial process, we keep only the basics.

Open your editor and type in the following code:

// Import the Modules installed to our server
var express    = require('express');
var bodyParser = require('body-parser');

// Start the Express web framework
var app        = express();

// configure app
app.use(bodyParser());

// where the application will run
var port     = process.env.PORT || 8080;

// Import Mongoose
var mongoose   = require('mongoose');

// connect to our database
// you can use your own MongoDB...