-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.2.js
More file actions
47 lines (40 loc) · 1.14 KB
/
Copy pathserver.2.js
File metadata and controls
47 lines (40 loc) · 1.14 KB
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const logger = require('morgan');
const errorhandler = require('errorhandler');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/my-first-db';
const app = express();
app.use(bodyParser.json());
app.use(logger('dev'));
app.use(errorhandler());
app.use(express.static(path.join(__dirname, '/')));
MongoClient.connect(url, function(err, db) {
console.log(db);
const studentObj = {
name: 'Gina',
age: '25'
};
const dataBase = db.db('my-first-db');
dataBase.collection('students').insertOne(studentObj, function (err, res) {
if (err) throw err;
console.log("文档插入成功");
db.close();
});
})
app.post('/transactions', (req, res) => {
console.log(req.body);
console.log(req.query.api_key);
res.send({'msg': 'transactions'});
})
app.use('/', (req, res, next) => {
// console.log(req);
next();
});
app.get('/accounts', (req, res) => {
res.status(200).send('get accounts');
})
const port = 7001;
app.listen(port);
console.log(`Your server is running in localhost:${port}`)