[英]nodeJS: Sending html data from one js to another
The code below is working 下面的代码正在工作
var express = require('express')
var app = express();
var fs = require('fs')
var addUserToDB = require('./addUserToDB')
app.use('addUserToDB', addUserToDB)
app.get('/register.html', function(req,res){
res.sendFile(__dirname+ "/" + "register.html");
})
var server = app.listen(8087,function(){
console.log("Listening at 8087");
})
app.get('/addUserToDB',function(req,res){
firstname = req.query.firstname;
console.log(firstname)
})
app.get('/register.html', function(req,res){
res.sendFile(__dirname+ "/" + "register.html");
})
However, when I try to remove the following method and place it into another .js file so I can get the firstName from that file. 但是,当我尝试删除以下方法并将其放置到另一个.js文件中时,我可以从该文件中获取firstName。 It's not working. 没用 The following code is in addUserToDB.js: 以下代码位于addUserToDB.js中:
var addUserToDB = app.get('/addUserToDB',function(req,res){
firstname = req.query.firstname;
console.log(firstname)
})
module.exports = addUserToDB;
I have tried making a addUserToDB.js file and added the code 我尝试制作一个addUserToDB.js文件并添加了代码
var express = require('express')
var app = express();
app.get('addUserToDB',function(req,res){
firstname = req.query.firstname;
console.log(firstname)
})
but it seems I am missing something because it doesn't work. 但似乎我错过了一些东西,因为它不起作用。 Thanks. 谢谢。
A few things here. 这里有几件事。 First, need to do a require
of addUserToDB.js
from server.js
(I will assume that's the name of your main file) and then use it as a middleware. 首先,需要从server.js
执行addUserToDB.js
的require
(我假设这是您的主文件的名称),然后将其用作中间件。 Also, you need to export the app from addUserToDB.js
. 另外,您需要从addUserToDB.js
导出应用程序。
server.js: server.js:
var express = require('express')
var app = express();
var fs = require('fs')
var addUserToDB = require('./addUserToDB');
app.get('/register.html', function(req,res){
res.sendFile(__dirname+ "/" + "register.html");
})
var server = app.listen(8087,function(){
console.log("Listening at 8087");
})
app.use(addUserToDB);
addUserToDB.js: addUserToDB.js:
var express = require('express')
var router = express.Router();
router.get('/addUserToDB',function(req,res){
firstname = req.query.firstname;
console.log(firstname)
})
module.exports = router;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.