Const express=the require (' express ')
Const app=express ()
Const bodyParser=the require (' body - parser)
App. Use (express. Static (' public '))
App. Use (bodyParser. Json ());
App. Use (bodyParser. Urlencoded ({extended: false}))
App. All (' * ', function (the req, res, next) {
Res. The header (" Access - Control - Allow - Origin ", "*");
Res. The header (" Access - Control - Allow - the Methods ", "PUT, GET, POST, DELETE, the OPTIONS");
Res. The header (" Access - Control - Allow - the Methods ", "X - Requested - With");
Res. The header (" Access - Control - Allow - the Methods ", "the content-type");
next();
});
//the fetch traditional way
App. Get ('/fetchdata '(the req, res)=& gt; {
Res. The send (' traditional method to obtain parameters' + the req. Query. Id)
})
//the fetch second way
App. Get ('/fetchdata: id, (the req, res)=& gt; {
Res. The send (' 2 methods for parameters' + the req. Params. Id)
})
App. Delete ('/fetchdata: id, (the req, res)=& gt; {
Res. The send (' delete method to obtain parameters' + the req. Params. Id)
})
App. Post ('/fetchdata '(the req, res)=& gt; {
Res. The send (' dpost method to obtain parameters' + the req. Body. The username + "-- -- -- -- -- -- -- -- -- --" + the req. Body. The password)
})
App. Get ('/a1, (the req, res)=& gt; {
SetTimeout (function () {
Res. The send (' a1)
}, 1000);
})
App. Get ('/a2, (the req, res)=& gt; {
SetTimeout (function () {
Res. The send (' a2 ')
}, 2000);
})
App. Get ('/a3, (the req, res)=& gt; {
SetTimeout (function () {
Res. The send (' a3)
}, 3000);
})
App. Get ('/data '(the req, res)=& gt; {
Res. The send (" Hello World ")
})
App. Get ('/data1, (the req, res)=& gt; {
Res. The send (' Hello Tom ')
})
App. Get ('/data2, (the req, res)=& gt; {
Res. The send (' Hello jerry)
})
App. Listen (' 3000 ', ()=& gt; {
The console. The log (" running ")
})
# # this is my HTML files, used to request js data, using the get method can successfully
<meta charset="utf-8" & gt;
<script SRC="https://bbs.csdn.net/topics/vue.js" & gt; </script>
<body>
<script>
//the fetch (' http://localhost:3000/a1 '). Then (function (data) {
////this necessary, used to get the background object
//return data. The text ();
//}). Then (function (data) {
//the console. The log (data);
//})
//the fetch the ginseng
//the fetch (' http://localhost:3000/fetchdata? Id=156 ', {
//method: 'get'
//}). Then (function (data) {
//return data. The text ();
//})
//. Then (function (data) {
//the console. The log (data);
//})
//second way (here with the get method can successfully)
//the fetch (' http://localhost:3000/fetchdata/456 ', {
//method: 'get'
//}). Then (function (data) {
//return data. The text ();
//})
//. Then (function (data) {
//the console. The log (data);
//})
//here with the delete method would have failed
Fetch (' http://localhost:3000/fetchdata/741 '{
Method: 'delete'
{}). Then (function (data)
Return the data. The text ();
})
Then (function (data) {
console.log(data);
})
</script>