First create a index.js, related to the introduction of middleware on the inside, without which the middleware, you need to enter index.js file in the folder where the nodejs install these modules, the installation command: npm install express express-static cookie cookie- session body-parser multer mysql, and created the www folder and views folder, www folder to put all css.js.img, etc. to be read, views all the folders delegated to render template ejs
Express the require = const ( "Express"); // frame const = expressStatic the require ( "Express-static"); // specified static document reading path const = the require cookie ( "cookie"); // parse cookie data const cookieSession the require = ( 'cookie-session'); // session-based cookie data is stored in makes request.session const = bodyParser the require ( "body-Parser"); // parse data post stored in the request.body const consolite = The require ( "consolite"); // template adapter const multer = The require ( "multer"); // file upload const MySQL = The require ( "MySQL"); // database
const db = mysql.createPool ({// establish a database connection Pooling
host: 'localhost ', // host address
name: 'root', // database account
password: 'q791469353' // database password
database: 'officialwebsite' // the name of the database
});
// first step, set up server and listen port var App = Express (); app.listen ( 8080); // where listening port 8080 // second step, a static document to read the specified path app.use (expressStatic ( ' ./www ')); // here designated read file in all static ./www // third step of initializing the session app.use (cookieParser (' asdhkj ')); // the cookie data analyzing var Array = []; // randomly generated array for encrypting the session for ( var I = 0; I <1000; I ++ ) { Array.push (Math.random ()); } app.user (cookieSession ({ name: 'name of the session ' , Keys: Array, // Keys expressed session encrypted character the maxAge: 60. 5 * 1000 * @ disposed session valid time, in milliseconds })); // fourth step, post parse pass over the data, stored in the request.body, express GET method to save the data frame in the request.query the app.use (bodyParser.urlencoded ({Extend: to false })); // fifth step, file upload data analysis app.use (multer ({dest: "save path file Upload"}) .ANY ()); // the any () represents a method of receiving a file upload any type // sixth step, the template adapted app.set ( ' "views Engine", "text / HTML"); // adapted template engine will recognize it as a html file app.set ( "views", "./ views"); // specify the template in views folder, the folder below to ejs as the end of the template, which syntax: <% js Code%> represents js code execution but not output, <% = js code%> represents execution code and outputs js, <% - js code%> indicates the escape code is output js, <% include 'other template path'%> represents the section of code introduced into the template in the template app.engine ( "html", consolite.ejs) ;// use ejs template engine =
// These are deploying applications built next to deal with the request to do
app.get ( "/", (request , response) => {// represents an access localhost: 8080 to make a response / time, request indicates the data transmitted by the browser, response data indicating the server in response to the browser
response.render ( "index.ejs", {}) ; // represents rendering index, ejs template, and transmitted to the object is an empty template
});
app.get ( "/ sign_in", (Request, Response) => {
var name = request.query.name; // get method of acquiring data transmitted over
db.query ( `SELECT * FROM xxx table WHERE field = $ {name}`, ( err, data) => {// query the database the parameters for the SQL statement, $ {variable} indicates the use of the variable in the string, then string should be anti-double quote: ``
// If an error occurs, the object err is not empty, otherwise empty object, and the data in the data found in
the throw IF ERR (ERR);
the else {
res.render ( "Sign-in.ejs", {data: data}); // to find the template data into data
}
});
});
app.post ( '/ Sign-up', (Request, Response) => {
var post = request.body; // Get post pass over the data
if (post.name == xx){
response.send ( 'xxx'); // after the code is successful, the function call to the callback method ajax distal pass parameters
};
app.get ( '/ Test', (Request, Response, Next) => {// to test, for example, with next () method of rendering a template test.ejs
IF (request.query.name == 'XXXX') {
next (); // perform a function the same as the next request
}
app.get ( '/ test' , (Request, Response) => {
the render ( '/ test.ejs', {});
});
});
});