-
Notifications
You must be signed in to change notification settings - Fork 341
Express App Good Old Form
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Medium Website | E-Mail
Write a route ('/form') that processes HTML form input (<form><input name="str"/></form>
) and prints backwards the str value.
To handle POST request use the post() method which is used the same way as get():
app.post('/path', function(req, res){...})
Express.js uses middleware to provide extra functionality to your web server.
Simply put, a middleware is a function invoked by Express.js before your own request handler.
Middlewares provide a large variety of functionalities such as logging, serving static files and error handling.
A middleware is added by calling use() on the application and passing the middleware as a parameter.
To parse x-www-form-urlencoded request bodies Express.js can use urlencoded() middleware from the body-parser module.
var bodyparser = require('body-parser')
app.use(bodyparser.urlencoded({extended: false}))
Read more about Connect middleware here:
https://github.com/senchalabs/connect#middleware
The documentation of the body-parser module can be found here:
https://github.com/expressjs/body-parser
Here is how we can flip the characters:
req.body.str.split('').reverse().join('')
When creating your projects from scratch, install the body-parser dependency with npm by running:
$ npm install body-parser
...in your terminal.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// create application/x-www-form-urlencoded parser
//var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(bodyParser.urlencoded({
extended: false
}))
// POST /form gets urlencoded bodies
app.post('/form', function(req, res) {
//if (!req.body) return res.sendStatus(400)
res.send(req.body.str.split('').reverse().join(''))
})
app.listen(process.argv[2]);
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.urlencoded({extended: false}))
app.post('/form', function(req, res) {
res.send(req.body.str.split('').reverse().join(''))
})
app.listen(process.argv[2])
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3