아핫뉴스실시간 인기검색어
아핫뉴스 화산 이미지
화산 아이콘 11
계엄은 잘못된 길입니다.
아하

생활

생활꿀팁

수줍은이구아나17
수줍은이구아나17

next.js React에서 mysql은 어떻게 사용하나요?

node자체에서 mysql을 사용하는건 되지만 아래 소스처럼 라우팅해주고 npm으로 실행시켜주는데요 해당 리액트소스에서는 require('mysql')가 안되더라구요

const http = require('http'); const https = require('https'); const { parse } = require('url'); const next = require('next'); const fs = require('fs'); const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev }); const handle = app.getRequestHandler(); const port1 = 3030; const port2 = 3080; const options = { key: fs.readFileSync('/etc/letsencrypt/live/react.snomet.site/privkey.pem'), cert: fs.readFileSync('/etc/letsencrypt/live/react.snomet.site/cert.pem'), }; app.prepare().then(() => { http .createServer((req, res) => { const parsedUrl = parse(req.url, true); const { pathname, query } = parsedUrl; handle(req, res, parsedUrl); }) .listen(port1, err => { if (err) throw err; console.log(`> Ready on http://localhost:${port1}`); }); https .createServer(options, (req, res) => { const parsedUrl = parse(req.url, true); const { pathname, query } = parsedUrl; if (pathname.indexOf('/test/chapters') === 0) { const a = pathname.replace('/test/chapters', ''); if (a.length > 0 && a.length < 3) { app.render(req, res, '/test/t' + a, query); } else { app.render(req, res, '/test/t1', query); } } else if (pathname === '/a') { app.render(req, res, '/about', query); } else if (pathname === '/i') { app.render(req, res, '/info', query); } else { handle(req, res, parsedUrl); } }) .listen(port2, err => { if (err) throw err; console.log(`> Ready on http://localhost:${port2}`); }); });
    1개의 답변이 있어요!
    • 제로초
      제로초

      리액트는 프론트엔드이기 때문에 당연히 require('mysql')이 안 됩니다.

      require('mysql') 부분은 위에 적어두신 app이 있는 부분에서 호출해서 사용하셔야 합니다. REST API 형식으로 URL 라우팅을 하세요. 익스프레스나 다른 프레임워크를 쓰면 코드가 더 깔끔해집니다.