Create a server and a method of connecting mongoose in the database nodejs

The first is to use the native way to create a local server, developers routine operations

// introduced nodejs http module comes with 
const = http The require ( "http" );
 // with http service and create a variable app receives the return value 
const app = http.createServer ();
 // add request request event app, req user request, RES, rendering the page, or is returned to the content page 
app.on ( "request", (req, RES) => {
     // res.end: returns the value of the final page 123 
    res.end ( " 123 " ) 
}) 
// App listening port 3000, developers are generally listening port 3000, can of course use other, but there is a range of values 
app.listen (3000, () => { 
    the console.log ( " local oN localhost running serve: 3000 " ) 
}) 
// enter the browser to http: // localhost: 3000 / can browse the contents of the local server,
// Of course, you must use the command line to start the server node
 
 
 
 
 

The second is to create a local server using the express module, because to use a third-party module, it is recommended that before creating the first input npm init -y, initialization packet node on the command line, and the system will automatically generate a file package.json, in addition Download other packages, the system will automatically generate another json file with a lock, these two documents Never, never, do not delete, package.json recorded in all the packages you have installed, package.lock. json recorded in your download package address, if you deleted, others get your project, it is easy to run up, or because the package version of the problem, there are all kinds of unexpected errors

// using the node command npm i express install third-party packages, and the introduction of 
const express = The require ( "express" );
 // call the method express, create a service 
const app = express ();
 // to app request to add an event, because it is express frame, the output change 
app.get ( "/", (REQ, RES) => {
     // this is not end because the native manner, the reading of the text-based, not coded if garbled 
    / / send usage with end, the element is returned to the page, but automatically send coded 
    res.send ( "Liuyuexue I" ) 
}) 
app.listen ( 3000, () => { 
    the console.log ( "Success" ) 
} )
// in the browser input http: // localhost: 3000 / can browse the contents of the local server, of course, have to use the command line to start the server node

The use of third-party packages mongoose mongoose database connection

// use mongoose connect to the server, of course, is the introduction of third-party packages 
const mongoose = The require ( "mongoose" );
 // connect others are the official package good, to get away with making 
// behind certain mongodb to add agreement, otherwise there will be problems 
mongoose.connect ( "MongoDB: // localhost / Blog") .then (CON => { 
    console.log ( "database successfully connected" ); 
}). the catch (ERR => { 
    Console .log ( "database connection failed" , ERR); 
}) 
// use manner because then, the database is connected to asynchronous operation, the object returns a promise 
// ( "MongoDB: // localhost / Blog", {useNewUrlParser: true, useUnifiedTopology: true})

If the return prompted a lot of text, and the last to suggest the cause "Database successfully connected", not an error, but a version of the reason, only the contents inside braces should prompt copied to connect on the line, the last line of the above comments It is to copy the template ...

Finally explain that I was learning the front, only a little bit of the back end of the base operation

Guess you like

Origin www.cnblogs.com/liuyuexue520/p/12158372.html