[英]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. 我试图使用Express将值从html的搜索框中获取到Nodejs中的索引路由文件。 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
到目前为止,我已经能够从搜索框中获取值到javascript / javascript.js文件,因此我只需要将该值传递给route / index.js文件
Use body-parser middleware to get the body of the POST request. 使用body-parser中间件获取POST请求的正文。 (I assume the name attribute of your search box is search_box_name and is posted to '/'.)
(我假设您的搜索框的名称属性是search_box_name,并发布到了“ /”。)
app.js 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 路线/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);
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.