Notice
Recent Posts
Recent Comments
Link
투케이2K
78. (NodeJs) [Mac Os] [Mvc] : Controll , Router , Server 패턴 구분 및 API 호출 수행 - 컨트롤 , 라우터 , 서버 본문
NodeJs
78. (NodeJs) [Mac Os] [Mvc] : Controll , Router , Server 패턴 구분 및 API 호출 수행 - 컨트롤 , 라우터 , 서버
투케이2K 2024. 1. 14. 09:27[개발 환경 설정]
개발 툴 : VS CODE
개발 언어 :NodeJs
[사전 설정]
[controlls : 소스 코드]
// ----------------------------------------------------------------------------------------------
const express = require('express')
const app = express()
// ---------------------------------------
app.set('view engine', 'ejs') // [Page] : [Render]
app.set('views', './views') // [Page] : [Render]
// ---------------------------------------
var bodyParser = require('body-parser'); // [body-parser 사용]
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ----------------------------------------------------------------------------------------------
// [Get] : Path = [/auth] : http://localhost:3000/login/auth
exports.auth = (req, res) => {
console.log("")
console.log("==============================================================================")
console.log("[Server] :: [Controlls] :: [Path = /login/auth] :: [Start]")
console.log("==============================================================================")
console.log("")
res.status(200).send(JSON.stringify({"result" : "success"})); // [http 반환]
}
// ----------------------------------------------------------------------------------------------
[routers : 소스 코드]
// ----------------------------------------------------------------------------------------------
const express = require('express')
const router = express.Router();
// ----------------------------------------------------------------------------------------------
const controll = require("../controlls/controll"); // [컨트롤 지정]
// ----------------------------------------------------------------------------------------------
// [Get] : Path = [/auth] : http://localhost:3000/login/auth
router.get('/auth', controll.auth); // [특정 컨트롤러 호출]
// ----------------------------------------------------------------------------------------------
// [router 지정]
module.exports = router;
// ----------------------------------------------------------------------------------------------
[server : 소스 코드]
// ----------------------------------------------------------------------------------------------
const express = require('express')
const app = express()
// ---------------------------------------
app.set('view engine', 'ejs') // [Page] : [Render]
app.set('views', './views') // [Page] : [Render]
// ---------------------------------------
var bodyParser = require('body-parser'); // [body-parser 사용]
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ---------------------------------------
const loginRouter = require('./routers/login'); // [라우터 지정]
app.use('/login', loginRouter);
// ----------------------------------------------------------------------------------------------
// [Get] : Path = [/] : http://localhost:3000/
app.get('/', function (req, res) {
console.log("")
console.log("==============================================================================")
console.log("[Server] :: [App] :: [Path = /] :: [Start]")
console.log("==============================================================================")
console.log("")
// [Web Return Response]
res.send('Hello World !!');
})
// ----------------------------------------------------------------------------------------------
// [Server] : [Start]
//*
app.listen(3000, function () {
console.log("")
console.log("==============================================================================")
console.log("[Server] :: [Port = 3000] :: [Start]")
console.log("==============================================================================")
console.log("")
})
// */
// ----------------------------------------------------------------------------------------------
[결과 출력]
반응형
'NodeJs' 카테고리의 다른 글
Comments