Nodejs personal website Building experience sharing--routing rule design (1)

Source: Internet
Author: User

Winter vacation in a company internship, do Nodejs platform under the Web development. The first learning Nodejs I feel this little thing is really potential. Use Nodejs to do server-side platform, the code is small, but has a strong variety of module support. So I'm going to build a personal website in my spare time. This serial article is used to exchange experiences and share the proceeds.

My personal website uses the Nodejs Platform Express framework with MySQL database. The Web front-end UI uses an open-source bootstrap framework.

The Express version is 4.11.0.

The site is generally composed of three main pages: blogs, projects, contact

The Blogs page displays the first paragraph of each article. Click on the page after the article called article

Plus the hidden login interface for administrator login.

The route of the site is initially planned like this:

The '/' and '/blogs ' paths are requested to be rendered using the Blog.ejs file.

'/login ', '/project ', '/contact ' three path requests are rendered using Login.ejs, Project.ejs, Contact.ejs three files respectively.

The '/blogs/articla/:articlenum ' path request is rendered using the Article.ejs file. The Articlenum variable represents the ID of each blog post.

The Projects page has not yet been designed.

In Blogs.ejs, the HTML code used to display each thumbnail article is as follows:

<Divclass=' Blog-container ' ><Divclass=' Row ' ><Divclass=' Col-lg-3 ' ><H5class=' Text-center ' ><%= blogsarray[i][' Blog_time '].toutcstring ()%></H5><H5class=' Text-center ' >readings:<%= blogsarray[i][' Blog_reading_number ']%></H5><H5class=' Text-center ' >author:<%= blogsarray[i][' Blog_author ']%></H5></Div><Divclass=' Col-lg-9 ' ><Ahref=<%= '/blogs/article/' +blogsarray[I]['blog_id ']%>><H3><%= blogsarray[i][' Blog_title ']%></H3></A><% var Textarr = blogsarray[i][' Blog_content '].split (/(?: \ r\n|\r|\n)/g); %><P><%= textarr[0]%></p> <% if (Textarr.length > 1) {%> <a href= <%= '/blogs/article/' + " Span class= "Hljs-attribute" >blogsarray[i][' blog_id ']% >>read More ... </a> <%}%> </div> </ div></DIV>    

The previous part of the three <%=...%> represents the template variable, we now ignore it, we look at the label section href , for the ‘/blogs/article/‘ connection with the blogsArray[i][‘blog_id‘] formation of a new url . In the app.js of Express, the code for this section is as follows:

app.get(‘/blogs/article/:blog_id‘, function(req, res, next){    connection.query(‘select * from blogs where blog_id = ‘ + req.params.blog_id, function(err, result) { if (err) { alert(‘Cannot get article‘); } else { res.article = result; } next() })})

In the index.js of routing control I use the following code:

router. Get ( '/blogs/article/:articlenum ',  Function (req, res, next) {Res.render ( ' article ', {blog:           

This code indicates that the /blogs/article/:articleNum file is used for rendering when it gets to the request article , and sets article the variable to pass the article data that is paged out from the database. The design of the database I will explain in detail later. The table used to store the article consists of the following 6 fields:
blog_id int not null auto_increment
blog_time datetime not null
blog_title mediumtext not null
blog_content longtext not null
blog_reading_number int not null
blog_author mediumtext not null

Nodejs personal website Building experience sharing--routing rule design (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.