回答編集履歴
1
詳しく説明、質問の意味を読み違えていました
answer
CHANGED
@@ -1,2 +1,31 @@
|
|
1
|
-
サーバーを作る場合→http(Nodeデフォルト)、[express](https://npmjs.com/package/express)など
|
1
|
+
サーバーを作る場合→http(Nodeデフォルト)、[express](https://npmjs.com/package/express)などが使えます。
|
2
|
+
httpの場合→
|
3
|
+
```js
|
4
|
+
const http = require("http");
|
5
|
+
const fs = require("fs");
|
6
|
+
|
7
|
+
http.createServer(function (req, res) {
|
8
|
+
const url = "public" + req.url;
|
9
|
+
if (fs.existsSync(url)) {
|
10
|
+
fs.readFile(url, (err, data) => {
|
11
|
+
if (err) throw err;
|
12
|
+
res.writeHead(200, {"Content-Type": "text/html"});
|
13
|
+
res.end(data)
|
14
|
+
});
|
15
|
+
}
|
16
|
+
});
|
17
|
+
```
|
18
|
+
「public」というフォルダを作り、そこにhtmlファイルを入れます。
|
2
|
-
|
19
|
+
([ここ](https://qiita.com/yamachan360/items/38a0d3c06eec1dfc3d6d)を参考にしました。ありがとうございます。)
|
20
|
+
expressの場合→
|
21
|
+
```js
|
22
|
+
const express = require("express");
|
23
|
+
app.engine('.html', require('ejs').__express);
|
24
|
+
app.set('views', __dirname + '/public');
|
25
|
+
app.set('view engine', 'html');
|
26
|
+
|
27
|
+
app.get("/", (req, res) => {
|
28
|
+
res.render("index")
|
29
|
+
));
|
30
|
+
```
|
31
|
+
これで、localhostにアクセスするとpublicフォルダの「index.html」が表示されます。
|