4.x



  • For example, there's a basic file, the main application looks like:

    var app = express();
    app.engine("ejs",require("ejs-locals"));
    app.set('view engine', 'ejs');
    app.set('views', path.join(__dirname, '../front_end/views'));
    app.use(express.static(path.join(__dirname, '../front_end/www')));
    app.use(favicon(path.join(__dirname, '../front_end/www/img', 'favicon.png')));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(session({
         //..
    }));
    

    Next, I want to use separate structures for the admin.

    var admin = express();
    admin.engine("ejs",require("ejs-locals"));
    admin.set('view engine', 'ejs');
    admin.set('views', path.join(__dirname, '../admin_front_end/views'));
    admin.use(express.static(path.join(__dirname, '../admin_front_end/www')));
    admin.use(favicon(path.join(__dirname, '../admin_front_end/www/img', 'favicon.png')));
    

    Then we'll connect the middleware:

    app.use(require("../middleware/userData"));
    admin.use(require("../middleware/userData"));
    

    And therums:

    require('../routes/index')(app,admin);
    

    The contents of the roll look like:

    module.exports = function(app,admin) {
    
     //"app" part
    
    app.use('/', require('routes/app/index'));
    app.use('/login', require('routes/app/login'));
    
     //"admin" part
    
    admin.use('/cp', require('routes/admin/admin'));
    

    };

    And the contents of the file. routes/admin/admin:

    var express = require("express");
    var router = express.Router();
    var Main = require("modules/main/controller");

    router.get("/", function(req, res) {
    var main = new Main(res,req);
    main.viewMainPage();
    });

    module.exports = router;

    And in the end, it doesn't work, I don't know how to come here. admin♪ Can you explain how to do the right thing?



  • There you go.

    var admin = express();
    // ...
    

    var app = express();
    app.use("/admin", admin);
    // ...

    PS Order important! If you need the application to be truly independent of the main application, the line app.use("/admin", admin); I need to write right after the announcement. app! And you certainly don't need to write any lines after the error processor.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2