You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Next we install body-parser package to help with processing JSON files passed in requests to the server. Use the following command: sudo npm install body-parser
Next we create the Books directory and navigate into it with the following command: mkdir Books && cd Books
Inside the Books directory initialize npm project and add a file to it with the following command: npm init Then add sever.js file with: vi server.js
In the server.js file, paste the following code:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
require('./apps/routes')(app);
app.set('port', 3300);
app.listen(app.get('port'), function() {
console.log('Server up: http://localhost:' + app.get('port'));
});
Install Express and set up routes to the server
Express will be used to pass book information to and from our MongoDB database and Mongoose will be used to establish a schema for the database to store data of our book register. To begin installation, type: sudo npm install express mongoose and enter.
while in Books folder, create a directory named apps and navigate into it with: mkdir apps && cd apps
Inside apps, create a file named routes.js with: vi routes.js
Then change the directory back up to Books using: cd ..
Now, start the server by running this command: node server.js If all goes well server should be up and running and we can connect to it on port 3300.
This however was not the case in my test, I kept on getting an error. see error in the picture below:
After several attempts at troubleshooting I figured out what the issue was. The issue was with my nodejs version. During the installation, I installed version 12 but for some reson version 12 won't work but keeps giving me errors when I attempt to start the sever.
I solved this problem by upgrading my node version to version, 17.0.0
After this I tried to start the server again and it ran successfully.
Next I accessed the HTML page over the internet via port 3300 using the public IP: http://34.200.223.160:3300/
Finally I enter some data into the database and it reflected.