Building the Real Application Chapter 5
const port = process.env.API_PORT || 8081;
app.use('/', router);
app.listen(port, function() {
console.log(`api running on port ${port}`);
});
Here, we have set up a server that tells the express server to run on the 8081 port. We will
be using this server to handle all the API requests via express.
Also, we have required and used all the packages that we need in this server.js file.
Also, for the mongoose connection, we have added a connection to our local database
called movie_rating_app with the following code block:
//connect to mongodb
mongoose.connect('mongodb://localhost/movie_rating_app', function() {
console.log('Connection has been made');
})
.catch(err => {
console.error('App starting error:', err.stack);
process.exit(1);
});
As I mentioned earlier, if the database does not exist yet, it will automatically get created
when we add our very first Mongoose document to the DB.
The next thing is to run our MongoDB server. Let's do that by running the following
command in the Terminal:
$ mongod
Once the Mongo server is up, let's spin up our node server for this application using the
following command:
$ node server.js
Now, when we open http://localhost:8081/, you should be able to see the following
message: