1. Configure the template engine
Express's default template engine is pug (jade). If you want to render the html page, you must import the corresponding template engine ejs.
npm install ejs
Installation is completed and the template engine is introduced in the file.
var ejs = require('ejs'); // Configure the Express view engine ('html', ejs.__express); ('view engine', 'html');
2. Configure page routing
If the page is not placed in the public directory, it must be accessed by configuring the route.
Suppose my file directory is as follows
|-views(In the root directory) |--mplat |---pages |---- |---
Configure global variables in
// Configure mplat rendering page('mplat',(__dirname,'views/mplat'))
This way, the mplat used elsewhere is equivalent to(__dirname,'views/mplat')
Create new in the routers directory and add two html files to the mapping
var express = require('express'); var router = (); /* GET mplat page. */ ('/', function(req, res, next) { ('mplat/', { title: 'DisCloudDisk' }); }); ('/console',function (req,res,next) { ('mplat/pages/', { title: 'Console' }); }) = router;
Introduce file routing in
('/mplat',require('./routes/mplat'));
After this configuration is completed, you only need to visit http://$host/mplat to return
3. Modify static file import
Define static file directory in
(((__dirname, 'public')));
To introduce css and js files on the page, you only need to add public before the default. The writing method is as follows
<script src="/lib/layui/"></script>
The actual directory is public/lib/layui/
4. Page routing
The jump of the html page also changes, and you need to register the corresponding interface in the route. For example, I access the console in the index, and the path is consistent with the registration in the route.
<iframe src="/mplat/console" frameborder="0" scrolling="yes" width="100%" height="100%"></iframe>
This is the end of this article about the implementation of Express configuring HTML page access. For more related Express HTML page access content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!