Home > Web Front-end > JS Tutorial > How to use Node.js to jump to html pages

How to use Node.js to jump to html pages

青灯夜游
Release: 2022-01-06 19:04:46
forward
5301 people have browsed it

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!

How to use Node.js to jump to html pages

Problem Description

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.

Project code structure

This small Demo mainly involves four files, including:

  • ##main.js: This part is the starting file, which is The entry file of the entire project;

  • main.html: This part is the html file of the main page;

  • new.html: To jump The html file of the page;

  • router.js: routing file, used to give specific operations based on the URL and parameters;

  • node_modules: Folder to store related modules.

Note: main.html and new.html are in the views folder.

Related module configuration

Use npm to install the following three modules respectively:

    express
  • art-template
  • express -art-template
Build main.js

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...')
})
Copy after login

Implemented monitoring of port 3000.

Build router.js

In 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  //暴露接口
Copy after login

Build main.html

Under this file, only one hyperlink is implemented to implement the page Jump, the

code part is as follows:

<div>
 <a href="/new" >页面跳转</a> <!--跳转至新页-->
</div>
Copy after login

Build new.html

This 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>
Copy after login

Run result

Enter the

command in the small black screen:

node main.js
Copy after login

The code runs successfully, open

http://localhost:3000

How to use Node.js to jump to html pages You can see a hyperlink to jump to the page. Click this hyperlink:

How to use Node.js to jump to html pages page No effective jump is achieved.

Problem analysis and solution

If you purely use the HTML language, you can directly jump to the hyperlink. After using the router, you should implement monitoring of the relevant URL to realize the jump. The goal.

So, add the following

code to router.js:

router.get(&#39;/new&#39;,function(req,res){
  res.render(&#39;new.html&#39;)
})
Copy after login

That is, when the URL is

localhost:3000/new, use res .render jump.

Since the html hyperlink is consistent with the link rendered by render, jumps using hyperlinks can be achieved.

The jump effect is as follows:

How to use Node.js to jump to html pages The problem is now solved!

For more node-related knowledge, please visit:

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template