post


NodeJS

First of all, to parse data, we need to install body-parser

npm install body-parser --save

form.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="uft-8">
    <title>email form</title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="author" href="humans.txt">
  </head>
  <body>
    <form action="/email_post" method="post">
      email : <input type="text" name="email"><br/>
      submit <input type="submit">
    </form>
    <script src="js/main.js"></script>
  </body>
</html>

app.js

var express = require('express')
var app = express()
// declare bodyParser like as above code.
var bodyParser = require('body-parser')

app.listen(3000, function() {
console.log("start!! express server on port 3000");
});

app.use(express.static('public'))
// code could be json style
app.use(bodyParser.json())
// code could be encoded
app.use(bodyParser.urlencoded({extended:true}))

//url routing
app.get('/', function(req,res){
res.sendFile(__dirname + '/public/main.html')
});

app.get('/main', function(req,res){
res.sendFile(__dirname + '/public/main.html')
});

// if any page call the /email_post
// server will print req.body.email in console
// in case of form.html, the information should be content in input text for e-mail form
app.post ('/email_post', function(req,res){
//get : req.parm('email')
console.log(req.body.email)
res.send("post response");
});





© 2017. by isme2n

Powered by aiden