-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (42 loc) · 1.3 KB
/
app.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
/* 1. expressモジュールをロードし、インスタンス化してappに代入 */
var express = require("express");
var app = express();
/* 2. listen()メソッドを実行して3000番ポートで待ち受け */
var server = app.listen(3000, function(){
console.log("Node.js is listening to PORT:" + server.address().port);
});
/* 3. 以後、アプリケーション固有の処理 */
// 写真 のサンプルデータ
var photoList = [
{
id: "001",
name: "photo001.jpg",
type: "jpg",
detaUrl: "http://localhost:3000/data/photo001.jpg"
},{
id: "002",
name: "photo002.jpg",
type: "jpg",
dataUrl: "http://localhost:3000/data/photo002.jpg"
}
]
// 写真リストを取得するAPI
app.get("/api/photo/list", function(req, res, next){
res.json(photoList);
});
// 選択した写真のデータを取得するAPI
app.get("/api/photo/:photoId", function(req, res, next){
var photo;
for(i = 0; i < photoList; i++){
if(photoList[i].id == req.params.photoId){
var photo = photoList[i];
}
}
res.json(photo);
});
// View EngineにEJSを指定
app.set('view engine', 'ejs');
// "/"へのGETリクエストでindex.ejsを表示する。拡張子.ejsは省略されていることに注意
app.get("/", function(req, res, next){
res.render("index", {});
});