Department of Computing: Lab 07: Express JS
Department of Computing: Lab 07: Express JS
Objectives
This lab will get you familiar with the Node Express JS environment.
Tools/Software Requirement
Node.js, Express Js, Notepad
Description
Installing Express
Firstly, install the Express framework globally using NPM so that it can be used to create a web
application using node terminal.
$ npm install express --save
The above command saves the installation locally in the node_modules directory and creates a
directory express inside node_modules. You should install the following important modules
along with express −
body-parser − This is a node.js middleware for handling JSON, Raw, Text and URL
encoded form data.
cookie-parser − Parse Cookie header and populate req.cookies with an object keyed by
the cookie names.
multer − This is a node.js middleware for handling multipart/form-data.
$ npm install body-parser --save
$ npm install cookie-parser --save
$ npm install multer --save
Hello world Example
Following is a very basic Express app which starts a server and listens on port 8081 for
connection. This app responds with Hello World! for requests to the homepage. For every other
path, it will respond with a 404 Not Found.
var express = require('express');
<body>
<p class = "greetings" id = "people">Hello World!</p>
</body>
</html>
Pug is capable of doing much more than simplifying HTML markup.
Task 1: Create Index.html file in the root folder of your application and write the HTML FORM
POST method code in it. Modify server.js to handle home page requests as well as the input sent
by the HTML form.
Hint: To handle HTTP POST request in Express.js version 4 and above, you need to install
middleware module called body-parser. The middleware was a part of Express.js earlier but now
you have to install it separately. This body-parser module parses the JSON, buffer, string and url
encoded data submitted using HTTP POST request. Install body-parser using NPM as shown
below.
Task 2: Create a file uploader form in an html file. This form has method attribute set to POST
and enctype attribute is set to multipart/form-data. Modify server.js to handle home page
requests as well as file upload.
Task 3: By using the Pug templating engine create an HTML registration form.
Solution
Task Code:
Deliverables
Compile a single word document by filling in the solution part and submit this Word file on
LMS. This lab grading policy is as follows: The lab is graded between 0 to 10 marks. The
submitted solution can get a maximum of 5 marks. At the end of each lab or in the next lab, there
will be a viva/quiz related to the tasks. You must show the implementation of the tasks in the
designing tool, along with your complete Word document to get your work graded. You must
also submit this Word document on the LMS. In case of any problems with submissions on
LMS, submit your Lab assignments by emailing it to Ms. Ayesha Asif:
ayesha.asif@seecs.edu.pk.