-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·78 lines (76 loc) · 2.29 KB
/
server.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
74
75
76
77
78
const express=require("express");
const app=express();
const mysql=require("mysql");
const users=require('./routes/api/users.js');
const course=require("./routes/api/course.js");
const student=require("./routes/api/student.js");
const teacher=require("./routes/api/teacher.js");
const stucho=require("./routes/api/stucho.js");
const teacho=require("./routes/api/teacho.js");
const bodyParser=require("body-parser");
const cors=require("cors");
const path=require('path');
const fs=require('fs');
app.use(cors());
//设置允许跨域访问该服务.
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
//设置路由,浏览器就可以访问到
app.get("/",(req,res)=>{
res.send("Hello world");
})
//使用body-parser中间件
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//使用routes 引入user.js
app.use('/api/users',users);
app.use('/api/course',course);
app.use('/api/student',student);
app.use('/api/teacher',teacher);
app.use('/api/stucho',stucho);
app.use('/api/teacho',teacho);
//静态资源的使用
app.use(express.static(path.join(__dirname, 'public')));
//DB ?
var db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '66825638155',
database : 'mystudb',
port:3306
});
//connected
// db.connect( err =>{
// if(err) {
// throw err;
// }
// console.log('mysql connected');
// })
//test
app.post("/register",(req,res)=>{
console.log(req.body);
})
app.get('/apidoc',(req,res)=>{
// fs.readFile('./apidoc/index.html','utf-8',(err,result)=>{
// if(err){
// throw err;
// }
// })
res.writeHead(200,{'Content-Type':'text/html'})
fs.readFile('./public/index.html','utf-8',function(err,data){
if(err){
throw err ;
}
res.end(data);
});
})
const port=process.env.PORT||5001;
app.listen(port,()=>{
console.log("Server in running on port"+port);
})