-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1 KB
/
index.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
const express = require('express');
const app = express();
const path = require('path');
const loggerMiddleware = function(req, res, next) {
console.log(`[${new Date()}] ${req.method} ${req.url}`);
next();
};
app.use(loggerMiddleware);
// 静的ファイルの提供とキャッシュの有効化
app.use(express.static('public', { maxAge: 86400000 }));
// test-imageリクエスト
app.get('/test-image', (req, res) => {
res.sendFile(path.join(__dirname, 'public/images/test.png'));
});
// GETリクエスト
app.get('/', (req, res) => {
res.send({
msg:'GET request'
});
});
// POSTリクエスト
app.post('/', (req, res) => {
res.send({
msg:'POST request'
});
});
// PUTリクエスト
app.put('/:id', (req, res) => {
res.send({
id: req.params.id,
msg:'PUT request'
});
});
// DELETEリクエスト
app.delete('/:id', (req, res) => {
res.send({
id: req.params.id,
msg:'DELETE request'
});
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});