Just understand Nodejs, found that Nodejs configuration is not complex, but there are a lot of places to pay attention to, today on record, later can also take out to see.
To complete this simple example, start from scratch and walk three steps.
I. Building a development environment
Two. Create a project (Express)
Three. Encoding
I belong to the technical class, no more nonsense.
I. Building a development environment
1. Nodejs
1.1 Installation
To the official website download the latest version of http://www.nodejs.org/, after downloading, install, all the way next
1.2 Configuring Path
Configure the above installed directory to the environment variable to make it easy to use Node.exe in the command line later.
For example my node.exe in "H:\Program Files\nodejs\node.exe", at the beginning of the environment variable with "H:\Program files\nodejs\;"
2. MongoDB
2.1 Installation
http://www.mongodb.org/downloads download, recommended download zip version, the development of the time, do not need to install, direct decompression can be used.
2.2 Start MongoDB using the command line method,
>mongod 127.0.0.1:27017--dbpath D:\mongodb\dbone//d:\mongodb\dbone MongoDB and Dbone folders to be set up first, in addition, the value of the--dbpath parameter can not be Space
Two. Create a project (Express)
1 Installing the global plug-in
>NMP Install-g Express //Automatic Download Express plugin
>nmp install-g express-generator //express tools in the command line
2. Add a Project
>EXPRESS-E MyProject
>CD MyProject
>NPM Install//Let NPM automatically download dependent packages based on Package.json
>NPM Install MONGO && NPM install Express-mongo//download MongoDB support Package
Three. Encoding
1. Modifying template rules
In App.js
App.set (' View engine ', ' Ejs ');
Modified to:
App.engine (' HTML ', require (' Ejs '). __express); App.set (' view engine ', ' html ');
Change the file suffix in/views/to *.html
2: Add a page (also called a view)
index.html Click "Login" to jump to login.html, log in work, jump to welcome.html, after welcome.html Click Logout to jump to login.html
/views/login.html
<!DOCTYPE HTML><HTML> <Head> <title><%=title%></title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1><%=title%></H1> <formMethod= "POST">User name:<inputtype= "text"name= "userid"ID= "userid"/><BR/>Password:<inputtype= "Password"name= "Password"ID= "Password"/><BR/> <inputtype= "Submit"value= "Login"/> <inputtype= "Reset"value= "Reset"/> </form> </Body></HTML>
/views/welcome.html
<!DOCTYPE HTML><HTML> <Head> <title><%=title%></title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1><%=title%></H1> <P>Welcome to:<%=userid%></P> <H1><ahref= "/logout">Cancellation</a></H1> </Body></HTML>
Modify/views/index.html
<!DOCTYPE HTML><HTML> <Head> <title><%=title%></title> <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/> </Head> <Body> <H1><%=title%></H1> <P>Welcome to<%=title%></P> <H1><ahref= "/login">Login</a></H1> </Body></HTML>
3. Add model
Add/routes/models.js
var mongo = require (' Mongoose '); var Schema = MONGO. Schema; var New Schema ({ userid:string, name:string, = Mongo.model (' User ', userschema);
4. Modifying routes
Modify/routes/index.js
varExpress = require (' Express ');varRouter =Express. Router ();varMONGO = require (' Mongoose ');//varModels = require ('./models ');//Introducing ModelvarUser =models. User;mongo.connect (' Mongodb://192.168.199.9:8888/logindb ');//connecting to a database/*GET home page.*/Router.get (‘/‘,function(req, res) {Res.render (' Index ', {title: ' Express ' });}); Router.get ('/login ',function(req, res) {//go to login pageRes.render (' login ', {title: ' Login ' });}); Router.post ('/login ',function(req, res) {//Handling Logon Requests varQuery_doc ={userid:req.body.userid, password:req.body.password}; User.count (Query_doc,function(err, doc) {if(doc = = 1) {//verify success, go to welcome pageRes.redirect ('/welcome?userid= ' +Query_doc.userid); }Else{res.redirect ('/login '); } }); }); Router.get ('/logout ',function(req, res) {//logout, go to login pageRes.redirect ('/login ');}); Router.get ('/welcome ',function(req, res) {//Welcome Page varUserID =Req.query.userid; Res.render (' Welcome ', {title: ', Userid:userid}); Module.exports= Router;
5. Running
5.1. Start App.js
>node app
5.2 Accessing Http://localhost:3000/in a browser
Tips:
1. Project if you want to support Chinese, the file encoding to use Utf-8, otherwise garbled
2. If you modify the source file, you want to see the effect immediately, we recommend installing the plugin Supervisor
>NPM install-g Supervisor//npm Auto-Download plugin
>supervisor app//install plugin to start website (formerly node app)
3. If the configuration is unsuccessful, the individual can provide remote assistance.
3.1 Teamview: [Email protected]
3.2 QQ: [email protected]
Validation message: Nodejs+mongodb
Source code Download
Nodejs&express+mongodb Complete Simple user login (i.e. Nodejs Getting started)