목록IT/Node.js (5)
BugDIARY
▶import 특정 기능이나 파일, 변수 등 export된 것의 기능을 사용하고 싶은 경우 import를 사용합니다. ▶export 특정 기능을 export하여 다른 곳에서 사용할 수 있게 합니다. 크게 두 가지 방법으로 사용할 수 있습니다. app.js export default app; - app파일 전체를 export합니다. init.js import app from "./app.js"; - init파일에서 export한 app파일을 불러와 app파일 안의 변수 및 함수를 사용합니다. router.js export const userRouter = express.Router(); userRouter.get("/", (req, res) => res.send('home index')); userRout..
▶Middleware사용하기 middleware는 요청과 서버 사이에서 실행하는 것을 말합니다. 미들웨어는 변수로서 request, response, next를 받을 수 있습니다. (가지고 있음) const middlewareHome = (req, res, next) => { console.log("middleWare View"); next(); } app.get("/", middlewareHome, handleHome); 간단히 Middleware를 확인할 수 있는 함수입니다. ① middlewareHome이라는 함수는 request, response, next라는 변수를 가지고 있으며 ② 이 변수에 값을 받게 되면 안의 console.log를 실행한 뒤 ③ next()를 반환시켜줍니다. ※ 현재의 미..
babel이란 대상 환경에 필요한 구문 변환 (및 선택적으로 브라우저)를 세부적으로 관리 할 필요없이 최신 JavaScript를 사용할 수있는 스마트 사전 설정입니다. babel을 설치함으로서 최신 JS코드를 작성할 수 있게 됩니다. 지금은 node에서 사용하는 babel을 설치하여야 하므로 아래와 같이 입력합니다. ▶ 설치 방법 npm install @babel/node PS [대상 프로젝트]> npm install babel npm WARN deprecated babel@6.23.0: In 6.x, the babel package has been deprecated in favor of babel-cli. Check https://opencollective.com/babel to support the..
○ Express를 설치하고 싶은 폴더 안에서 아래와 같은 커맨드 입력 npm install express PS [프로젝트경로]> npm install express npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open '[프로젝트경로]\package.json' npm WARN PublicRecipe No description npm WARN PublicRecipe No repository field. npm WARN PublicRecipe No README data npm WARN PublicRecipe No li..
app.js import "core-js"; import express from "express"; import morgan from "morgan"; import helmet from "helmet"; import cookieParser from "cookie-parser"; import bodyParser from "body-parser"; const app = express(); app.use(cookieParser()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); app.use(helmet()); app.use(morgan("dev")); export default app; - nodejs의 경우, e..