In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Node.js to jump to html pages". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Problem description
Recently in the use of Node.js and html to learn the relevant knowledge of the page, when learning the page jump, there is the problem of unsuccessful jump, recorded here for future reference.
In Node.js, you mainly use the express framework, and the front end uses html.
Project code structure
The mini Demo mainly involves four files, including:
Main.js: this section is the starting file and the entry file for the entire project.
Main.html: this section is the html file of the main page
New.html: the html file to jump to the page
Router.js: route file, which is used to give specific operations based on URL and parameters
Node_modules: the folder where the related modules are stored.
Note: main.html and new.html are under the views folder.
Related module configuration
Use npm to install the following three modules:
Express
Art-template
Express-art-template
Build main.js
The code section 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...')})
The monitoring of port 3000 is realized.
Build router.js
In this file, we mainly create routing instances, monitor URL and related parameters, and render the relevant interface.
The code section is as follows:
Const express = require ('express') / / create routing instance const router = express.Router () router.get (' /', (req,res) = > {res.render ('main.html')}) module.exports = router / / expose interface to build main.html
Under this file, only one hyperlink is implemented to realize the jump of the page. The code section is as follows:
Page jump to build new.html
This file is very simple, only one line of output statement is used to indicate the success of the jump, the code section is as follows:
The result of jump operation is successfully realized.
Enter the command on the small black screen:
Node main.js
The code runs successfully and opens http://localhost:3000:
You can see the hyperlink that appears on the jump page, click on this hyperlink:
The page did not jump effectively.
Problem analysis and solution
If you purely use URL language, you can directly achieve the jump of hyperlinks, after using router, you should achieve the goal of monitoring the relevant HTML in order to achieve the goal of jump.
So, add the following code to router.js:
Router.get ('/ new',function (req,res) {res.render ('new.html')})
That is, when URL is localhost:3000/new, use res.render to jump.
Because the hyperlink for html is consistent with the link for render rendering, you can jump using hyperlinks.
The effect of the jump is as follows:
This is the end of the content of "how to use Node.js to jump to html pages". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.