简体   繁体   中英

Sending and Retrieving Form data with Node Js

I have already created two js files each of which sends and returns form data to/from the database. My question is if its better/possible to leave the two Js files as is and to serve the individual functions or if its better practice to combine them into a singular Js file. The two POST requests are like so for clarity.

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile('existing.html', { root:__dirname});
});

//Node.js body parseing middleware; used for req.body

app.post('/submit',urlencodedParser,function (req, res){
connection.connect(function (err) {
console.log(req.body.lname);
var lname = req.body.lname;

var sql = "SELECT * FROM users WHERE lname= '" + lname + "'";
connection.query(sql, function(err,result,rows) {
if (err) throw err;
console.log(result);

As per my understanding you need help regarding better way of organizing your Express.js code and routers. If I am not wrong then following tutorials will help you in that.

  1. An Intuitive Way To Organize Your ExpressJS Routes http://www.codetunnel.io/an-intuitive-way-to-organize-your-expressjs-routes/

  2. Quick Tip: Organizing Routes in Large Express 4.x Apps https://start.jcolemorrison.com/quick-tip-organizing-routes-in-large-express-4-x-apps/

Thanks, Jignesh Raval

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM