Open3

nodeの調査

西川信行西川信行
node_test.js
const http = require("http")
const fs = require("fs")
const URL = require("url").URL

const indexPage = fs.readFileSync("./html/index.html", "UTF-8")
const styleCss = fs.readFileSync("./html/css/style.css", "UTF-8")
const scriptJs = fs.readFileSync("./html/js/script.js", "UTF-8")

const hostname = "127.0.0.1"
const port = 3000

const server = http.createServer((req, res) => {
  const url_parts = new URL(req.url, 'relative:///')
  switch (url_parts.pathname) {
    case "/":
    case "/index.html":
      res.writeHead(200, { "Content-Type": "text/html" })
      res.write(indexPage)
      res.end()
      break
    case "/css/style.css":
      res.writeHead(200, { "Content-Type": "text/css" })
      res.write(styleCss)
      res.end()
      break
    case "/js/script.js":
      res.writeHead(200, { "Content-Type": "text/plain" })
      res.write(scriptJs)
      res.end()
      break
    default:
      res.writeHead(200, { "Content-Type": "text/plain" })
      res.end("お探しのページは見つかりません。")
      break
  }
})

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`)
})
index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>こんにちは、のおど・じぇいえす。</title>
    <link rel="stylesheet" href="css/style.css" />
    <script src="js/script.js"></script>
  </head>
  <body>
    <section>
      <h1>こんにちは、のおど・じぇいえす。</h1>
      <p>Node.jsでHTMLタグを作れるのかテストですと。</p>
    </section>
  </body>
</html>
style.css
h1 {
  color: red;
}
script.js
alert("こんにちは、のおど・じぇいえす!");
西川信行西川信行
index.js
const express = require("express")
const app = express()

app.use(express.static('public'));

app.get("/", (req, res) => {
  // res.sendFile(__dirname + "/public/index.html")
})

app.listen(3000, () => {
  console.log("Start server port:3000")
})