By Jeskson
Source: front end tavern of dada
Request and upload files
GET and POST requests
const express = require('express'); const app = express(); app.set('view', 'pug'); app.get('/', function(req,res){ // Output response message req.send('hello') }); app.get('/users/:gender', function(req,res){ // Get route parameters let gender = req.params.gender; // Get url parameters let name = req.query.name; // Output response res.send('gender='+gender+',name='+name); }); // Start HTTP server app.listen(8080, function(){ console.log('express') });
The POST request gets the parameters through the body parser template engine and the req.body() function.
const express = require('express'); const bodyParser = require('body-parser'); // Create express program const app = express(); // Configure view templates app.set('view engine', 'pug'); // Configure post app.use(bodyParser.urlencoded({ extended: true }); app.get('/create', function(req, res){ res.render('create.pug'); }); app.post('/create', function(req,res){ // Get user output form message let name = req.body.name let pswd = req.body.password; res.send('name='+name+",pswd="+pswd); }); app.listen(8080, function(){ console.log('express'); });
Upload files
Upload files with the media middleware:
const express = require('express'); const bodyParser = require('body-parser'); const multer = require('multer'); const app = express(); app.set('view', 'pug'); app.use(bodyParser.urlencoded({ extended:true })); const upload = multer({ dest: 'uploads/' }); app.get('/create', function(req, res){ res.render('create.pug'); }); app.post('/create', function(req,res){ // Get user output form information let name = req.body.name; let pswd = req.body.password; res.send('name =' + name + ",pswd="+pswd); }); app.get('/upload', function(req, res){ res.render('upload.pug'); }); app.post('/upload', upload.single('photo'), function(req,res){ // Get upload file information let file = req.file; res.send(file); }); // Start http server app.listen(8080,function(){ console.log(); });
node server.js
session Introduction
The server will create a session object for the browser. A browser can only have one session. Session is like this. When the user data needs to be maintained, the server program can store the user data in the session of the browser. When the user accesses other programs using the browser, the data can be extracted from the session.
Use of session:
npm install express-session var session = require("express-session"); app.use(session({ secret: 'keyboard cat', resave: true, saveUninitialized: true }))
Differences between cookie s and session s:
Cookie is to write the user's data to the user's browser and keep the state data at the browser end. When accessing the server, the amount of data transferred is large and the burden is heavy. The user may modify the cookie information, which leads to the server's insecurity.
Session is to write the user's data to the user's session. Different users are identified by different session ID, and keep the session ID in the client's cookie or locally. When the request is sent, the cookie information of the session "ID is attached to distinguish which user's data.
Introduction to restful api
REST infrastructure:
Restful API to create a json data resource file.
var express = require('express'); var app = express(); var fs = require("fs"); app.get('/listUsers", function(req,res){ fs.readFile(dirname + '/' + "users.json", "utf8" ,function(err,data){ console.log(data); res.end(data); }); }) var server = app.listen(8081,function(){ var host = server.address().address var port = server.address().port console.log(); })
node server.js
Nodemon
nodemon to monitor changes to the node.js application and automatically restart services.
npm install -g nodemon
npm install --save-dev nodemon
nodemon common commands:
nodemon -h or nodemon --help Use help
nodemon --watch path
Monitor current work path
nodemon --ignore
Ignore that some files are monitored
rs
Manual start system
❤ don't forget to leave your footprints of learning [like + collect + comment]
Author Info:
[author]: Jeskson [original public account]: front end tavern of dada. [welfare]: reply to "materials" on the public account and send the self-study materials gift bag (share in the group, say what you want, see if I have it)! [reprint description]: please explain the source of reprint, thank you for your cooperation! ~
Large front-end development, positioning front-end development technology stack blog, PHP background knowledge point, web full stack technology field, data structure and algorithm, network principle, etc. are easy to understand and presented to small partners. Thank you for your support, thank you very much!!!
If the content of this number is not in place (for example, involving copyright or other issues), please contact us in time for rectification, and we will deal with it as soon as possible.
Please like it! Because your approval / encouragement is the biggest driving force of my writing!
Welcome to your attention. Dada CSDN of!
This is a blog with quality and attitude