1. Write it in front
Everyone wants to become a full stack coder. As a front-end web developer, the simple road to full stack seems to be node.js. I learned node.js some time ago to talk about how novices can quickly build their own web services and start the road to full stack.
2. Install node.js
Anyone who has been exposed to back-end development knows that the first thing to do is to install the service. As a novice, you must choose the simplest visual installation (the next step of the fool, other methods, etc., will naturally know it after familiarizing with relevant operations). Download the computer-adapted installation package (this is Windows, I can't afford a Mac), and then install it according to the boot. It is installed by default under the C:/Program Files/nodejs file, and add this directory to the PATH environment variable. For specific practices, right-click "My Computer"-"Properties"-"System Advanced"-"Advanced"-"Environment Variable"-Select "Variable Name: PATH"; "Change the value of the variable: Add [C:/Program Files/nodejs] at the end (depending on your installation directory)". Open cmd and run the command directly:
node -v can output the current version number. npm has been integrated into the node file, and then use npm install XXX to install the required plug-ins or modules.
3. Use the express framework
After working hard for a while, I finally used the npm command to initialize, install the express framework, and then wrote hello world to enjoy it. Why choose the Express framework? Of course, it has its special features. What newbies are most afraid of is that they are troublesome and easy to make mistakes. Express certainly took it into consideration for us, so it provided a quick generator: express-generator
1. Install to global via command: npm install express-generator -g
2. Use the express command to generate the project structure
express myapp where myapp is your project name
3. Enter the project file through cd myapp
Initialize dependency modules via npm install
Start the web server by setting DEBUG=myapp & npm start
4. Open the http://localhost:3000/ URL in the browser and you can see this application.
By default, the template engine used is Jade, and this template has been configured in the project.
4. Introduction to the project of express generator
1. The organization of the myapp project is as follows:
2. package.json can be said to be the module management package, project information and module version number. In fact, you will find that when the project module is initialized, it is generated by the configuration here.
3. app.js is the project startup file, which can be said to be the core of the project. Mainly write some public functions.
4. There is a www file without suffix under the bin file. This is the entry file of the project, which configures the web service port and some listening events.
5. node_modules is a file module that depends on the project. The imported package will be placed in it later, such as the mongoose module connecting to the database, which will be discussed in detail later.
6. Public is a static resource file set of the project. It is easy to see that the pictures, css files, and js files are all placed here.
7. routes is the project's routing module, where the index.js and user.js files have been defaulted. This actually includes controller content in general background languages, and of course it can be separated on large projects.
8. Views is the template file of the project and is a Jade template engine. This template is very concise, but there are many pitfalls. For example, the requirements for spaces are very strict. One more or one less space will report errors. I have stepped on many pitfalls, but in fact its performance is not very high, so it is better to use ejs.
5. Install mongoDB
1. Also download the msi file directly on the official website (http://www.mongodb.org/downloads )
2. The next step is to install it. If there is a default, let it default. If there is a choice, choose all.
3. Then configure the environment variables, just like the node, no longer describe them, but you can put them in the picture, hahaha...
4. Next is to start the mongoDB service
5. Pass the command: mongod --dbpath f:/MongoDB/data where f:/MongoDB/data is the file storage path. I saw the following information and explained it was successful.
6. MongoDB listens to port 27017. At the same time, when opening the browser and entering http://127.0.0.1:27017 , you will see the following prompt:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
7. Then, open a cmd and enter the mongo command to link the database. The following prompt appears:
2015-05-02T17:10:19.467+0800 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.0.2 connecting to: test
8. In this way, monogDB is installed successfully in the Windows environment.
Replenish:
9. If you think it is troublesome to open a service with commands every time, you can write it into a batch file, which means creating a new file with the suffix.bat and write the following code:
start mongod --dbpath F:/MongoDB/data
10. Of course, you can also start MongoDB in a service manner, but I think it is not very useful in the learning process. Friends can try it yourself. If you need it, I will make up for it later.
11. If you think the command line is not easy to use, recommend a software with a graphical interface: MongoVUE, which is similar to navicat. It has a free version, which means there are fewer functions, but it is completely sufficient during the learning process.
6. Use monogDB in node project
1. Import the monogDB connection module. Express officially introduces the mongoskin module. I won’t talk about this. Here we introduce the installation through mongoose
2. Run the npm install mongoose -save installation in myapp project and save it to node_modules. You can also configure "mongoose": "^4.4.12" in package.json, and then command npm install to install.
3. In the app.js file
a. Import the mongoose module:
var mongoose = require('mongoose');
b. Create a database connection
mongoose.connect('mongodb://localhost/myDB') //Connect the local database
4. Create a new folder schemas in the project root directory. This is the dataset module. Create a new users.js file under the module.
var mongoose = require('mongoose');//Declare a mongoons object var UsersSchema = new mongoose.Schema({ name: String, paw: String, meta: { createAt: { type: Date, default: Date.now() }, updateAt: { type: Date, default: Date.now() } }})//Every execution, it will be called, and the time update operation UsersSchema.pre('save', function(next) { if(this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); }else { this.meta.updateAt = Date.now(); } next();})//Search static method UsersSchema.statics = { fetch: function(cb) { //Query all data return this .find() .sort('meta.updateAt') //Sorting.exec(cb) //Callback}, findById: function(id, cb) { //Query a single piece of data based on id return this .findOne({_id: id}) .exec(cb) }}//Exposed method module.exports = UsersSchema5. Add modules file to the root directory. This is the data model module. Add users.js file under the module.
var mongoose = require('mongoose') var UsersSchema = require('../schemas/users') //Get the exported dataset module var Users = mongoose.model('Users', UsersSchema) //Collection and generate the Movie model module.exports = Users6. Add routing controller code to users.js file in routes file
var express = require('express');var mongoose = require('mongoose');//Import mongoose module var Users = require('../models/users');//Import model data module var router = express.Router();/* GET users listing. */router.get('/', function(req, res, next) { res.send('respond with a resource');});//Query all user data router.get('/users', function(req, res, next) { Users.fetch(function(err, users) { if(err) { console.log(err); } res.render('users',{title: 'user list', users: users}) // Here you can also directly return data in json format res.json({data: users}); })}) module.exports = router;7. Add users.jade to the views file
extends layoutblock content h1= title //jade value method ul each user in users //jade template traversal method li h4 #{user.name} span #{user.paw}8. Finally, open the URL: http://localhost:3000/users/users in the browser to view the effect. Here, a project from the database to the front-end is completed.
The above is all about this article, I hope it will be helpful to everyone's learning.