Im trying to take the value from a search box in html to the index route file in Nodejs using express. So far i was able to take the value from the search box to the javascript/javascript.js file so i just need to pass that value to the routes/index.js file
Asked
Active
Viewed 38 times
0
-
You can refer to this: http://stackoverflow.com/questions/5710358/how-to-get-post-a-query-in-express-js-node-js – quanfoo Aug 20 '15 at 12:40
1 Answers
0
Use body-parser middleware to get the body of the POST request. (I assume the name attribute of your search box is search_box_name and is posted to '/'.)
app.js
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
require('./routes/index')(app);
app.listen(3000);
module.exports = app;
routes/index.js
module.exports = function(app) {
app.post('/', function(req, res) {
var val = req.body.search_box_name; // POST body is attached to req.
console.log(val);
});
};

Supasate
- 1,564
- 1
- 16
- 22