大家好,如何从一个路由文件到另一个路由文件使用变量?

时间:2020-07-06 06:30:53

标签: javascript html jquery node.js express

personalinfo.js

var express = require("express"),
router = express.Router(),
personalinfo = require("../models/personalinfo");
// To show PersonalInfo 
router.get("/new", function(req, res) {
res.render("personalinfo/new");
});
// To create personalInfo date
router.post("/new", function(req, res) {
const personalInfo = req.body.personalinfo;
console.log(personalInfo);
personalinfo.create(req.body.personalinfo, function(err, ninfo) {
    if (err) {
        res.send(err);
    } else {
        res.redirect("/objective/new");
    }
});
});
module.exports = router;

obj.js

var express = require("express"),
router = express.Router(),
objective = require("../models/objective");

router.get("/new", function(req, res) {
res.render("objective/new");
});

router.post("/new", function(req, res) {
objective.create(req.body.objective, function(err, nobj) {
    if (err) {
        res.send(err);
    } else {
        res.send(nobj);
    }
});
});

module.exports = router;

从personalinfo.js路由文件中,我想在obj.js路由文件中使用personalInfo变量。我怎样才能做到这一点。谢谢你帮我:)

1 个答案:

答案 0 :(得分:0)

您可以在节点中使用模块。

npm install connect-flash
var express = require('express');
var flash = require('connect-flash');
var app = express();
app.use(flash());


app.get('/login', function(req, res){
  req.flash('profileInfo', 'SomeText')
});

app.get('/profile', function(req, res){

  let message = req.flash('profileInfo')
  res.render('index', { message: message });
});

More info about NPM package