Download presentation
Presentation is loading. Please wait.
Published byMegan Roxanne Riley Modified over 6 years ago
1
Week 04 (Final Project) http://fog.ccsf.edu/~hyip
Node.js Week 04 (Final Project)
2
Installing Express for final project
Create a new directory final_project which is under the nodejs_workspace directory. (mkdir final_project, then cd final_project). Use the npm init command under the new final_project folder to create a package.json file for your application – final_project. npm init NOTE: you can hit RETURN to accept the defaults for most of them, except entry point is app.js: entry point: app.js Install express locally in the nodejs_workspace directory, and save it in the dependencies list: npm install express --save Install body-parser middleware, which is responsible for handling Raw, JSON, URL, and Text encoded form data: NOTE: body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through `req.body` npm install body-parser --save Install multer middleware, which is responsible for the handling of multipart/form-data: npm install multer --save
3
Handle get method We are going to use the Form GET method in HTML so as to pass two values. The input will be handled by the router “process_get” inside the file “server.js” Steps to complete the project: Create index.html. Create server.js. Start the server: node server.js Run the program from browser:
4
Create index.html <html> <head><title>this file is used with server.js</title> </head> <body> <h1>Process get method</h1> <form action=" method="get"> Your First Name: <input type="text" name="fname"> <br> Your Last Name: <input type="text" name="lname"> <br> <input type="submit" value="Process Get method"> </form> </body> </html>
5
Create server.js var express = require('express'); var app = express(); app.get('/index.html', function (req, res) { res.sendFile(__dirname + "/index.html"); }); app.get('/process_get', function (req, res) { var response = { fname: req.query.fname, lname: req.query.lname }; console.log(response); // res.end(JSON.stringify(response)); res.json(response); var server = app.listen(8081, function () { console.log('server.js is listening at or
6
Handle post method We are going to use the Form GET method in HTML so as to pass two values. The input will be handled by the router “process_post” inside the file “server.js” Steps to complete the project: Update index.html. Update server.js. Start the server: node server.js Run the program from browser:
7
Update index.html Add the following codes to the index.html <hr>
<h1>Process post method</h1> <form action=" method="post"> Your First Name: <input type="text" name="fname"> <br> Your Last Name: <input type="text" name="lname"> <br> <input type="submit" value="Process Post method"> </form>
8
Update server.js Add the following codes to the server.js
var bodyParser = require('body-parser'); // creating the application/x-www-form-urlencoded parser var urlenParser = bodyParser.urlencoded({ extended: false}); app.post('/process_post', urlenParser, function (req, res) { var response = { fname: req.body.fname, lname: req.body.lname }; console.log(response); res.end(JSON.stringify(response)); });
9
References Node.js the Right Way, Jim R. Wilson, The Programatic Bookshelf, ISBN NodeJS: Practical Guide for Beginners, Matthew Gimson, ISBN Express.js: Node.js Framework for Web Application Development, Daniel Green, ISBN Express.com Tutorials Point The Node Beginner Book, Manuel Kiessling, Leanpub, Link.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.