简体   繁体   English

如何将value变量从javascript文件夹中的普通js文件转换为nodejs中的route / index.js文件并进行表达?

[英]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.

相关问题 在快速应用程序中,公用文件夹中的javascript文件如何访问index.js下路由文件夹中编写的数据库API - In an express application ,how can the javascript file in public folder access the database API written in routes folder under index.js 将变量值从 index.js 访问到 nodejs 上的另一个 js 文件 - Accessing variable value from index.js to another js file on nodejs node.js如何从index.js执行其他js文件? - nodejs how to execute other js file from index.js? 将数组从 javascript 文件发送到 Express 中的 index.js - Send array from a javascript file to index.js in Express 如何在node / mean / express应用程序中拆分index.js文件? - How do I split up index.js file in a node/mean/express application? 使用Express 4将数组从Nodejs中的app.js传递到route / index.js - Pass array to routes/index.js from app.js in Nodejs with Express 4 如何将输入从玉器文件传输到index.js文件? - How do I transfer input from a jade file to my index.js file? 在Express.js应用中排除来自index.js文件的默认路由 - Exclude default routes form index.js file in Express.js app 使用 index.js 从“/文件夹”导入 javascript - javascript import from '/folder' with index.js 如何配置Brunch.io将index.js文件从app /复制到public / - How do I configure Brunch.io to copy the index.js file from app/ to public/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM