javascript - i am not able to fetch data from html controls using get method in nodejs and mongodb -
problem in code
var userid = req.userid; var pwd = req.pwd; console.log("userid = " + userid + "pass = " + pwd);
the console shows values undefined instead of input data
the console shows values undefined instead of input data
i want take data html file , insert mongo database using method. not able fetch data textbox.
code in nodejs(index.js)
const express = require('express'); const path = require('path'); const bodyparser = require("body-parser"); const mongoose = require('mongoose'); const app = express(); app.use(bodyparser()); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json()); app.set('port', (process.env.port || 1000)); mongoose.connect('mongodb://localhost/trackdb'); var schema = new mongoose.schema({ username: string , pass: string }); var users = mongoose.model('users', schema); app.get("/register", function (req, res) { var userid = req.userid; var pwd = req.pwd; console.log("userid = " + userid + "pass = " + pwd); new users({ username: userid , pass: pwd }).save(function (err, doc) { if (err) { res.json(err); } else res.send("successfully registerd!"); }); console.log("users = " + users); }); app.get("/", (req, res) => { res.sendfile(path.join(__dirname, './public/index.html')); }); app.listen(1000, () => { console.log("server start......"); });
html page (index.html)
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>login/register</title> <style> #container { width: 40%; margin: auto; border: 1px solid; padding: 10px; padding-left: 200px; border-radius: 10px; } </style> </head> <body> <div id="container"> <h1>register</h1> <form action="/register"> <div id="register"> <input type="text" name="userid" id="txt_userid" placeholder="enter user id"> <br> <input type="password" name="pwd" id="txt_pass" placeholder="enter password"> <br> <br> <button type="submit" id="btn_register">register</button> </div> </form> <h1>login</h1> <form action="/login"> <br> <br> <div id="login"> <input type="text" name="user" id="userid" placeholder="enter user id"> <br> <br> <input type="password" name="passw" id="pass" placeholder="enter password"> <br> <br> <button type="submit" id="btn_login">login</button> </div> </form> <h1 id="msg"></h1> </div> </body> </html>
try code
var userid =req.query.userid; var pwd = req.query.pwd
Comments
Post a Comment