-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
74 lines (53 loc) · 1.88 KB
/
backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require("express");
var https = require("https");
var fs = require("fs");
var str='{"id":"123",name:"jack",arg:11111}';
function onRequest(request, response){
console.log("Request received.");
response.writeHead(200,{"Content-Type":'text/plain','charset':'utf-8','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});//可以解决跨域的请求
//response.writeHead(200,{"Content-Type":'application/json', 'Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});
//response.write("Hello World 8888\n");
str=fs.readFileSync('data.txt');
response.write(str);
response.end();
}
http.createServer(onRequest).listen(3000);
console.log("Server has started.port on 3000\n");
console.log("test data: "+str.toString());
// var app = express();
// app.use(express.static("./frontend"));
// app.listen(3000);
// console.log("Express app running on port 3000");
// module.export == app;
// var options = {
// hostname: "en.wikipedia.org",
// port: 443,
// path: "/wiki/George_Washington",
// method: "GET"
// };
// var req = https.request(options, function(res) {
// var responseBody = "";
// console.log("Response from server started.");
// console.log(`Server Status: ${res.statusCode} `);
// console.log("Response Headers: %j", res.headers);
// res.setEncoding("UTF-8");
// res.once("data", function(chunk) {
// console.log(chunk);
// });
// res.on("data", function(chunk) {
// console.log(`--chunk-- ${chunk.length}`);
// responseBody += chunk;
// });
// res.on("end", function() {
// fs.writeFile("george-washington.html", responseBody, function(err) {
// if (err) {
// throw err;
// }
// console.log("File Downloaded");
// });
// });
// });
// req.on("error", function(err) {
// console.log(`problem with request: ${err.message}`);
// });
// req.end();