Nodejs + mongodb project details

Source: Internet
Author: User
Tags install mongodb

Nodejs + mongodb project details
1. Preparations for Nodejs, express, and mongodb installation (the latest version is installed here) ### installation ### there is nothing to say about Nodejs installation. However, after the installation is complete, you may have some problems if you have successfully used the original version. The original version is: $ node-v is not supported in the new version $. After you install the latest express version in the command line: express-generator // express command tool. Many beginners will encounter this problem (of course I am also. Use express to create a project framework. I created a website named chihou. The template engine is ejs.> Express-e chihuo-e indicates the ejs template engine. If you do not write-e, the default jade template engine is created, and then enter: cd chihuo // enter the project directory you created npm install // read the package in the root directory. the json file then installs the packages that the project depends on and then uses the cmd command line cd To Go To The chihuo directory> node app to start the project. Tip 1: If the project cannot be started: Check that there is no listening port in the app. js file. If no, in module. exports = app; Add the app before the statement. listen (3000); prompt 2: Ctrl + c to stop running. Then, you can enter http: // 127.0.0.1: 3000/or http: // localhost: in the address bar of the browser: 3000 this is your first node app created by express. Now you have completed an important step of the project. Next we will explain how to start from the directory -- the package on which the node_modules project depends -- the directory where public resources are stored -- routes learning name routing, there are some routing files in it -- views is the page file -- app. js project entry file. You can also change it to another name. 2. now the basic results are available. After that, we will create a database and install mongodb. Here we will only introduce the installation of windows and download the zip file on the official website. This is the one I installed in the mongodb directory on the d disk. create a mongodb folder on disk D, decompress the downloaded package, and copy the bin folder to the created mongodb folder. create a data folder in the mongodb folder, and then create a db folder in the data folder. open CMD command line> d:> cd mongodb \ bin> mongod-dbpath D: \ mongodb \ data \ db4. open a CMD command line:> d:> cd mongodb \ bin> ipv5. you can use it now. Then we will design our database. In the mongodb database we just opened, enter:> use chihuo \ To create a database named chihuo> db. createCollection ("users") \ create a collection> db. users. insert ({"name": "admin", "password": "111"}) \ Add a document to the users set.> Db. users. find () \ QUERY the document you added, connect to the database you just created in the project, create a folder database under the root directory of the project, and then create a db. js copy code 1 var mongoose = require ('mongoose'); 2 var db = mongoose. createConnection ('mongodb: // localhost/chihuo'); //; Connect to database 3 var Schema = mongoose. schema; // create model 4 var userScheMa = new Schema ({5 name: String, 6 password: String7}); // defines a new model, however, this mode is not associated with the users set. user = mongoose. model ('users', userScheMa); // corresponds The ers set is associated with the copy code. 3. Create a view file in the views folder. The project View File Created with express is the extension name of ejs. We generally use the extension name of html. So how can we let him identify html view files? In the app. in the js file, find> app. set ('view engine ', 'ejs'); replace it with:> app. set ('view engine ', 'html'); and then use the app. the engine () method registers the extension name of the template engine. Code:> app.engine('.html ', require ('ejs '). _ express); // The underline in it is two. After that, we create a login.html (login page, index.html (originally available, just change the suffix), ucenter (login page), and copy code 1 login.html 2 <! DOCTYPE html> 3

Related Article

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.