Closed2
vite + React SSR Server, postgres接続
概要
vite + SSR + postgresのメモになります
- 前の、vite SSRに、DB連携まわり 追加する形になります
環境
- vite
- express
- React
- postgres
作成したコード
- 関係パス
- /api/de_test
- config.ts に、接続先等を追加します
- src/routes/test.ts
test.ts
de_test: async function(client: any, req: any, res: any): Promise<any>
{
let item = {};
let result: any = {};
const retObj = {ret: "NG", data: [], message: ''};
try {
if (req) {
const text = `
SELECT * FROM public."Task" ORDER BY id DESC LIMIT 100
`;
result = await client.query(text);
//console.log(resulte);
if(result.rows.length< 1) {
console.error("Error, results.length < 1");
throw new Error('Error , get');
}
}
client.end();
return ({ret: Config.OK_CODE, data: result.rows});
} catch (error) {
throw new Error("error, de_test");
}
},
- package.json
{
"name": "vite_ssr_react3",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "node server",
"build": "npm run build:client && npm run build:server",
"build:client": "vite build --outDir dist/client",
"build:server": "vite build --ssr src/entry-server.jsx --outDir dist/server",
"generate": "vite build --outDir dist/static && npm run build:server && node prerender",
"serve": "NODE_ENV=production node server",
"debug": "node --inspect-brk server"
},
"dependencies": {
"pg": "^8.11.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.16.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.1.0",
"compression": "^1.7.4",
"express": "^4.18.2",
"serve-static": "^1.15.0"
}
}
このスクラップは2023/10/07にクローズされました