생활
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개의 답변이 있어요!