Realization of student information to add functionality
in the form template request address specified in the request mode
for each individual table to add a name attribute
to add routing functions to achieve student information
student information transfer from the receiving client
add student information to the database
will be redirected to the page student information list page
The project development project development process by pressing: under views folder is a folder template model is manipulating files into the database interface files and create file folders route is the route to get the code public place-based public css file app.js file
app.js
// introduced http module const = http the require ( 'http' ); // introduced into the template engine const = the require Template ( 'Template-Art' ); // introduction path module const = path the require ( 'path' ); // introduced static resource access module const = serveStatic the require ( 'serve-static'); // returns a method // into the process date third-party modules const = DateFormat the require ( 'DateFormat' ); const Router = the require ( './ route / index ' ); // implement static resource access service const serve = serveStatic (path.join (__ dirname,' public ')) // return a method // configure root templates = path.join template.defaults.root (__ dirname, 'views' ); // method for processing date format template.defaults.imports.dateformat = DateFormat; // database connection the require ( './ Model / Connect' ); / / create server const App = http.createServer (); // when the client access server app.on ( 'Request', (REQ, RES) => { // enable the routing function router (req, res, ( ) => {}) // enable static resource access service functions serve (REQ, RES, () => {}) }); // port to listen app.listen (3000 ); console.log ( 'server startup success' );
Database moudel folder Code
user.js
const mongoose = require('mongoose'); // 创建学生集合规则 const studentsSchema = new mongoose.Schema({ name: { type: String, required: true, minlength: 2, maxlength: 10 }, age: { type: Number, min: 10, max: 25 }, sex: { type: String }, email: String, hobbies: [ String ], collage: String, enterDate: { type: Date, default : Date.now } }); // Create a set of student information const = Student mongoose.model ( 'Student' , studentsSchema); // the set of student information derived module.exports = Student;
connect.js
the require Mongoose = const ( 'Mongoose' ); // connect to the database mongoose.connect ( 'MongoDB: // localhost / WRJ', {useNewUrlParser: to true }) .then (() => the console.log ( 'database connection success' )) . the catch (() => the console.log ( 'database connection failed'))
Routing Code
// introduced router module const = getRouter the require ( 'router' ); // Get the route object const = router getRouter (); // Student information set const = Student the require ( '../ Model / User' ); // introduced template engine const template = The require ( 'Art-template' ); // introduced querystring module const querystring = The require ( 'querystring' ); // presenting student file information page router.get ( '/ add', ( req, res) => { the let HTML = Template ( 'index.art' , {}); res.end (HTML); }) // presenting student profile information list page router.get ( '/ list', async (req, res) =>{ // query the student information the let Students. = The await Student.find (); the console.log (Students.); The let HTML = Template ( 'list.art' , { Students.: Students. }) Res.end (HTML) }) // implemented student routing information addition function router.post ( '/ the Add', (REQ, RES) => { // received request parameters post the let formData = '' ; req.on ( 'Data', param => { formData + = param ; }); req.on ( 'End', the async () => { the await Student.create (querystring.parse (formData)) RES.writeHead(301, { Location: '/list' }); res.end() }) }); module.exports = router;