Last active
February 4, 2018 19:14
-
-
Save vickonrails/d81a6351a6b75bf4119c9f0070d0fba4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //server.js | |
| const express = require('express'), | |
| app = express(), | |
| //You must require the body-parser middleware to access request.body in express | |
| bodyParser = require('body-parser'); | |
| //configuring bodyparser | |
| app.use(bodyParser.json()); | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| //setting our port | |
| app.set('port', process.env.PORT || 3000); | |
| //Get route for localhost:3000 | |
| app.get('/',(request,response)=>{ | |
| response.sendFile(__dirname +'/form.html'); | |
| }); | |
| //POST route for form handling | |
| app.post('/',(request,response)=>{ | |
| console.log(request.body); | |
| response.send(`${request.body.name} said ${request.body.message}`); | |
| }); | |
| app.listen(3000,()=>{ | |
| console.log('Express server started at port 3000'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //form.html file | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset='UTF-8'> | |
| <title>Form page</title> | |
| <style> | |
| *{ | |
| margin:0; | |
| padding:0; | |
| box-sizing: border-box; | |
| font-size: 20px; | |
| } | |
| input{ | |
| margin:20px; | |
| padding:10px; | |
| } | |
| input[type='text'], | |
| textarea { | |
| width:200px; | |
| margin:20px; | |
| padding:5px; | |
| height:30px; | |
| display: block; | |
| } | |
| textarea{ | |
| resize:none; | |
| height:60px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <form action='/' method='POST'> | |
| <input type='text' name='name' placeholder='name'/> | |
| <textarea name='message' placeholder='message'></textarea> | |
| <input type='submit'/> | |
| </form> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment