forked from Onion4070/nodejs_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (25 loc) · 974 Bytes
/
Copy pathserver.js
File metadata and controls
35 lines (25 loc) · 974 Bytes
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
const express = require("express");
const app = express();
const userRouter = require("./routes/user");
const PORT = 3000;
// 静的レンダリング
// app.use(express.static("public"));
// 動的レンダリング
app.set("view engine", "ejs");
app.get("/", (req, res) => {
// サーバー側のログに表示
// console.log("Hello express");
// クライアント側に表示
// res.send("<h1>こんにちは</h1>");
// ステータスコードを表示
// res.sendStatus(404);
// ステータスのメッセージをデフォルトから変更
// res.status(404).send("エラーです");
// jsonでも返せる
// res.status(500).json({msg: "エラーです"});
// 本来ならデータ部分はデータベースなどから取得
res.render("index", {text: "NodejsとExpress"});
});
// ルーティング
app.use("/user", userRouter);
app.listen(PORT, () => console.log("サーバーが起動しました"));