简体   繁体   中英

How do i take the value variable from a normal js file in the javascript folder to the routes/index.js file in nodejs and express?

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

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);
    });
};

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