Table of Contents
Problem Description
Project code structure
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

Jan 06, 2022 pm 06:50 PM
node.js Page jump

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Nov 18, 2023 pm 05:08 PM

Detailed explanation of PHP page jump functions: Page jump techniques for header, location, redirect and other functions, which require specific code examples. Introduction: When developing a Web website or application, jumping between pages is an essential function. PHP provides a variety of ways to implement page jumps, including header functions, location functions, and jump functions provided by some third-party libraries, such as redirect. This article will introduce in detail how to use these functions

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

Use uniapp to achieve page jump animation effect Use uniapp to achieve page jump animation effect Nov 21, 2023 pm 02:15 PM

Title: Using uniapp to achieve page jump animation effect In recent years, the user interface design of mobile applications has become one of the important factors in attracting users. Page jump animation effects play an important role in improving user experience and visualization effects. This article will introduce how to use uniapp to achieve page jump animation effects, and provide specific code examples. uniapp is a cross-platform application development framework developed based on Vue.js. It can compile and generate applications for multiple platforms such as mini programs, H5, and App through a set of codes.

An article to talk about how to efficiently develop presentation layer Node.js applications An article to talk about how to efficiently develop presentation layer Node.js applications Apr 17, 2023 pm 07:02 PM

How to use Node.js for front-end application development? The following article will introduce you to the method of developing front-end applications in Node, which involves the development of presentation layer applications. The solution I shared today is for simple scenarios, aiming to allow front-end developers to complete some simple server-side development tasks without having to master too much background knowledge and professional knowledge about Node.js, even if they have no coding experience.

See all articles