Home > Article > Web Front-end > How to use Node.js to jump to html pages
How to use Node.js to jump to pages? This article will introduce to you how to implement html page jump based on Node. I hope it will be helpful to you!
Recently, I am using Node.js and html to learn the relevant knowledge of the page. When I learn the page jump, , there was a problem of unsuccessful jump, recorded here for future reference.
In Node.js, the express framework is mainly used, and html is used on the front end.
This small Demo mainly involves four files, including:
The code part is as follows:
const express = require('express') const app = express() const router = require('./router') app.engine('html',require('express-art-template')) app.use(router) app.listen(3000,() => { console.log('successful...') })Implemented monitoring of port 3000. Build router.jsIn this file, we mainly create routing instances, monitor URLs and related parameters, and render related interfaces.
The code part is as follows:
const express = require('express') //创建路由实例 const router = express.Router() router.get('/',(req,res) => { res.render('main.html') }) module.exports = router //暴露接口Build main.htmlUnder this file, only one hyperlink is implemented to implement the page Jump, the
code part is as follows:
<div> <a href="/new" >页面跳转</a> <!--跳转至新页--> </div>Build new.htmlThis file is very simple, just use a line of output statement to indicate the successful jump,
The part of the code is as follows:
<div> <th>成功实现跳转</th> </div>Run resultEnter the
command in the small black screen:
node main.jsThe code runs successfully, open
http://localhost:3000:
You can see a hyperlink to jump to the page. Click this hyperlink:
page No effective jump is achieved.
code to router.js:
router.get('/new',function(req,res){ res.render('new.html') })That is, when the URL is
localhost:3000/new, use res .render jump.
The problem is now solved!
nodejs tutorial! !
The above is the detailed content of How to use Node.js to jump to html pages. For more information, please follow other related articles on the PHP Chinese website!