'Backend/node.js' 카테고리의 글 목록
본문 바로가기

Backend/node.js

(22)
Node 초간단 업데이트 방법(MAC과 Window) Mac 언제나 새로시작하는 프로젝트 시작은 신난다! Nextjs최신버전으로 프로젝트를 하려고하니까 이런 메시지가 떴다. You are using Node.js 18.12.1. For Next.js, Node.js version >= v18.17.0 is required. 스택오버플로우 검색해보니 노드 다운받고안해도된다고해서 간편하게 업그레이드를했다. ## (force) clear you npm cache sudo npm cache clean -f ##install n (this might take a while) sudo npm install -g n ##upgrade to the current stable version sudo n stable https://stackoverflow.com/questi..
[nodejs] 미들웨어(Middleware) 사용법 노드js에서 미들웨어란 웹 애플리케이션에서 요청과 응답사이에 위치하여 클라이언트의 요청을 처리하고 필요한 작업을 수행하도록 도와준다. 클라이언트로부터 들어오는 HTTP요청과 서버로부터 클라이언트로 보내는 HTTP 응답에 영향을 주는 중간 소프트웨어 구성 요소로 req, res객체 그리고 라우터 함수 사이에 존재한다. 장점 코드의 모듈성과 재사용성을 높이며 코드를 보다 구조적으로 유지하고 유지보수를 용이하게 만들어준다. 사용방법 app.use() 또는 router.use()를 통해 등록하고 next()함수를 호출하여 다음 미들웨어로 제어를 넘길 수 있다. index.js const app = express(); app.use((req, res, next) => { req.country = 'KR' next..
fetch 에러 SyntaxError: Unexpected end of JSON input fetch의 catch에서 자꾸 에러가 잡혔다. 클라이언트 문제라고생각해서 요청헤더를 수정해봤는데 고쳐지지않았다. 찾아보니 서버문제였다. 원인 서버에서 전송된 json데이터가 비어있거나 올바르게 형식화 되어있지 않았을 때 발생한다. API를 확인해보니 성공시 다음과같이 리턴해주고있었다. return res.status(200).json() 성공상태를 나타내는 success를 속성을 추가하여 해결하였다. return res.status(200).json({ success: true });
Nodejs index.js 예시 1. ejs를 사용할경우 index.js const express = require("express"); const cors = require("cors"); const app = express(); app.set("view engine", "ejs"); require("dotenv").config(); const {port} = ensureEnv('PORT') app.use(cors()); // app.use(cookieParser()); app.use(express.static("public")); app.use(express.json({ limit: "50mb" })); app.use(express.json()); app.use(express.urlencoded({ extended: false, l..
[nodejs] Uncaught SyntaxError: Unexpected token '<' UncatchSyntaxError: Unexpected token '
[nodejs] Fatal javascript OOM in GC during deserialization $ npm install -g increase-memory-limit $increase-memory-limit increase-memory-limit를 설치후 실행하면 node_modules안에 .bin폴더내 많이 쌓였던 파일들을 삭제해서 메모리를 확보하는듯하다. 저장공간을 늘리는 방법도 있던데 이것은 좀더 공부하고 업데이트해야할듯.
Router 추가하기 추가+ 백엔드 정보를 뷰에 옮기는법 router.use((err, req, res, next) => { res.status(err.status || 500); res.render('error',{//error.ejs를 열고 errCode를 전달 errCode: err.status }) }); //방법1: html에서 불러오기 //방법2:자바스크립트에서 불러오기 ----- 기존 프로젝트가 있다고 가정함 메인 index.js ... router.use("/sample/", require("./router/sample.js")); ... [router] sample.js const express = require('express') const config = require('../config') const m..
nodejs Router로 원하는 데이터 가공(get, post) let gameData; axios({ method: 'get', url: 'https://blabla' }) .then(function (response) { gameData = response.data; }) .catch(function (error) { console.log(error); }); 여기 한 데이터가 있다.(글로벌로 되어있어 router내부에서 사용가능함) 이 데이터속 deco라는 key값에 해당하는 데이터를 가져와 router 호출시 뿌려주려한다. 즉 원하는 데이터가공. 나는 post를 여태껏 수정할때만 사용하는 줄 알았었는데 get처럼 사용할수있다고한다. 아마 1,2,3의 데이터를 받아다가 데이터를 가공하여? json형태로 되돌려줄때 사용하는듯하다. 1.Get router //w, ..