我在node.js中新我頭疼,因爲我不能讓如何通過在node.js中新手任務的NodeJS - 路由/文件之間傳遞數據
我的文件文件/路由之間的數據結構看起來像這樣(很簡單的流行結構):
ROUTES (folder)
--form.js
--test.js
VIEWS (folder)
--form.ejs
--test.ejs
index.js
package.json
index.js文件(標準明確應用程序文件):
var express = require('express');
var body_parser = require('body-parser');
var form = require('./routes/form.js');
var test = require('./routes/test.js');
var app = express();
app.set('view engine', 'ejs');
app.use(body_parser.urlencoded({extended:true}));
app.use('/', form);
app.use('/test', test);
var port = 3700;
var server_listen_on = app.listen(port, function()
{
console.log('Server is listening on port: ' + port);
});
form.ejs文件:
個<!DOCTYPE html>
<html>
<head>
<title>pole</title>
</head>
<body>
Hi! Put some web address here:</br>
<form action="/post" method="post">
<input type="text" name="address">
<input type="submit" value="ENTER OR CLICK HERE">
</form>
</body>
</html>
test.ejs文件:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
The page you fill in:<br>
<%= target %><br>
Let us see how it looks like:<br>
</body>
</html>
form.js文件
var express = require('express');
var body_parser = require('body-parser');
var router = express.Router();
router.get('/', function(req, res, next)
{
res.render('form'); // this is rendering our form - as a start page
});
router.post('/post', function(req, res, next)
{
var data = req.body.address; // user fill the form (test.ejs file)
res.render('test', {target : req.body.address}); // we grab the data that user put in the form and render test page that will show this data
module.exports.page = data; // now I want to pass it to other js file - test.js
});
var page = function()
{
console.log('just for test purpose');
};
module.exports = router; // right now module.exports is equal to router (express.router).
// How to pass some more data in it (like above function - page) - something like module.exports = page + router; So I could use page variable in other js file[?]
test.js文件:
var express = require('express');
var request = require('request');
var router = express.Router();
var req_formjs = require('./form.js');
console.log(req_formjs.data); // undefined... [?] how to pass variables from form.js to test.js [?]
router.get('/', function (req, res, next)
{
request.get(target, function (err, res, body) // putting target in here (as a url) isn't working... [?] how to pass variable from router.post in form.js to router.get in test.js [?]
{
if (!err && res.statusCode == 200)
{
res.render('test', {content : body}); // second render of test.ejs is not working [?]
}
else
{
console.log('error');
}
});
});
module.exports = router;
更簡單的(答案),就更好了。代碼中包含一些問題 - 以便更好地理解。如果有任何教程或類似的問題(我沒有檢查,我找不到任何描述),只需粘貼鏈接,謝謝。
編輯:讓我們用更簡單的形式更好地理解。問題是如何以最簡單的模式將變量傳遞給其他js文件(沒有任何路由只是使用module.exports)。
index.js - 同
form.js
var express = require('express');
var body_parser = require('body-parser');
var router = express.Router();
var preciousData = 'var precious_data - from form.js file';
module.exports =
{
exportsPreciousData : preciousData,
router : router
};
test.js
var express = require('express');
var request = require('request');
var router = express.Router();
var requireFormjs = require('./form.js');//({exportsPreciousData : exportsPreciousData});
//console.log(requireFormjs.exportsPreciousData); // I guess console.log is faster then module.exports... but
router.get('/', function(req, res, next)
{
res.render('welcome', {test : requireFormjs.exportsPreciousData});
});
// why this doesn't work - I need to use middleware function - could you provide simple working example?
//(I guessing that even if console.log print data after module.exports machinery I would still need middleware, right?)
module.exports = router;
welcome.ejs
<!DOCTYPE html>
<html>
<head>
<title>welcome</title>
</head>
<body>
<%= test %>
</body>
</html>
...爲什麼它不工作(沒有e xpress一切運行良好)?
如果你想做一些處理然後調用一個將數據傳遞給你的路由器的函數,你想用'data'做什麼。如果您需要用於以後的用戶請求,那麼您需要進行會話。 –