您的位置:首页 > Web前端 > Node.js

URL Distribution About NodeJS

2017-08-15 10:46 309 查看
  Long time no see , guys . Been very busy recently . But I didn't stop coding . I just had no time to share with you . Now I have a little time to write this blog . 

  This time I want to talk about URL distribution with NodeJS . As we all know JS aka JavaScript is a language to handle web actions . It is born for web in the f
4000
irst place , not for server . But finally it can be used to develop server , it is called NodeJS
. You can see it as a library of JS which can run in server like php . Now I am about to begin . 

  What is URL distribution ? We know we just need to input the url in the url input field of the browser . like : https://www.baidu.com , or https://auth.st.gmcc.net/dana/home/infranet.cgi . And then we enter , and we get some files like html , css or js
. But what happen back there , the server . And all this is your job if we need or want to build a server by your own . And URL distribution is about doing something when you get the url , and it is just a string , nothing special , like I just mentioned . 

  So first thing you need to do is " understand " WTH it means . We can ignore the host address . All we need to pay attention to is behind the ' / ' symbol which are routes and behind the ' ? ' symbol which are request parameters . So We need to get them
by calling split function to split the string into an array . 

  In my code , we split the url and the we can get the method , uri , parameters ... : 



  But you need to make some rules about url , like length or index of parameters so you can know what that means . In my code , I make the rule : the format of url is " /ControllerName/uri ". it is totally up to you . You just need to make them in JS files
and deal them in server files . I deal them in main.js file . 

  Then , I want to talk about the whole project . What it includes ? How it works . 

  The directories of the project is : 



As you can see , it has two directories named Server and Web . The first contains the server files . The second contains web files . I am not gonna talk about the Web directory . I am gonna talk about the Server directory . It has three sub directories named
Controllers , Modules and Users and one JS file named main . First thing is the JS file . It is an entry of the server . All of requests must enter in this file . You just need To write " node main.js " command in the terminal under the path of main.js to
start the server . And you can input your local address in the browser and enter , you can get the request in main.js file . 

  And the whole server project is built by using MVVM pattern . The Controllers directory contains controller JS files , like User.js which handle the user business . And the Module directory contains module Js files , like SQL.js which handle the SQL database
business . And the Users directory , it is just a file which contains my data files . You can ignore it . 

  OK ! This is how the server work . Hope you can understand it . And you can also download the code . have a good day ~ 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: