http서버 활용

969·2021년 12월 29일
//httpServer.js
const express = require("express")
const bodyParser = require("body-parser")
const { getVersion, getBlocks } = require("./chainedBlock.js")
const { Blocks, addBlock, nextBlock, } = require("./checkVaildBlock")



const http_port = process.env.HTTP_PORT || 3001

function initHttpServer(){
        const app = express()
        app.use(bodyParser.json())

        app.get("/blocks", (req,res)=>{
                res.send(getBlock())
        })

        app.post("/mineBlock", (req, res)=>{
                const data = req.body.data || []
                const block = nextBlock(data)
                addBlock(block)

                res.send(block)
        })

        app.get("/version", (req,res) =>{
                res.send(getVersion())
        })
        app.post("/stop", (req, res)=>{
                res.send({"msg":"Stop Server!"})
                process.exit()

        })

        app.listen(http_port, ()=>{
                console.log("Listening Http Port : " + http_port)
        })


}
initHttpServer();

HTTP 포트 확인

env || grep HTTP_PORT
포트 3001번추가

추가된포트 확인

node httpServer.js &
& 쓰면 백그라운드로 실행됨

express 모듈이 없다고 오류가 떠서

npm i express body-parser express 해서 깔아줬다.

다시 node httpServer.js & 해서 실행시키면

3001번으로 실행되고 있다고 나온다.

ps -ef 해서 보면 PID 11487번으로 돌아가고 있는걸 확인

0개의 댓글