First, build the development environment
Go to http://nodejs.org Download development environment
http://Expressjs.com Download Install express NPM install-g Express
continue installing Ejs: NPM Install Ejs
If you want to run a node. JS program, you can now only use "Node App.js", and this way of running, if the App.js file after modification often requires a reboot to load the new content, which is very inconvenient for development, for this reason, A supervisor component package can be used, which dynamically loads the modified development program.
Download and install Supervisor
NPM install-g Supervisor
Run the program using the Supervisor method
Supervisor App.js
Second, code development
Using the Ejs template
All users need to save the page should be saved in the views directory, but at this time Index.ejs is an HTML page, and some people are not accustomed to using EJS, accustomed to the use of HTML, in order to solve such problems, you need to configure the template.
Defining the Loaded Project module
var Ejs = require (' Ejs '); The Require () function represents the module to be loaded |
Need to add some related configuration;
App.engine (' HTML ', ejs.__express); App.set (' View engine ', ' html '); Replacement: App.set (' View engine ', ' Ejs '); |
Modify the Index.ejs file in views to the Index.html file to see if it can be changed.
Below write a user's login, log in when only provide user ID (userid) and password, the following set up a few related Operations page: login.html, welcome.html. However, when creating these two files, it is important to note that the encoding of the file must be set to UTF-8, or the Chinese will not be able to operate.
<!DOCTYPE HTML><HTML> <Head> <title><%=title%></title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1><%=title%></H1><formMethod= "POST">User id:<inputtype= "text"name= "userid"ID= "userid"><BR>Dense Code:<inputtype= "Password"name= "Password"ID= "Password"><BR> <inputtype= "Submit"value= "Login"> <inputtype= "Reset"value= "Reset"></form></Body></HTML>
login.html
<!DOCTYPE HTML><HTML> <Head> <title>Welcome</title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1>Welcome<%=User.userid%>To!</H1> <H1><ahref= "Logout">Cancellation</a></H1> </Body></HTML>
welcome.html
<!DOCTYPE HTML><HTML> <Head> <title><%=title%></title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1><%=title%></H1> <P><ahref= "Login">User Login</a></P> </Body></HTML>
index.html
There is no direct connection defined in each page, because node. JS is still part of the MVC design pattern, so for the implementation of MVC, all that is written here belongs to the map name, and all mapping paths need to be routed through App.js for the corresponding routing configuration.
Third, design the routing function
Nodejs + MongoDb