multer (postgresql)? ???? ???? ????? ?? Nodejs Express REST API? ???? ??
Jan 25, 2025 am 12:32 AMNode.js, Multer ? PostgreSQL? ???? ??? ??? ??? API ??
? ????? Node.js, Multer ? PostgreSQL? ???? ?? ? ?? ??? ??? ??? ???? ???? ???? ?? API? ???? ??? ?????. ??? ?? ?? ??? ???? ?????? ??? ?? ??? ? ????.
?? ??:
- Node.js? PostgreSQL? ?? ? ???????.
- Node.js ? PostgreSQL? ?? ??? ?????.
- API ???? ?? Postman.
???? ??:
-
???? ???? ?? ? ???:
mkdir imageUpload cd imageUpload npm init -y
-
??? ??:
npm install express multer body-parser pg
-
???? ?? ???:
<code>imageUpload/ ├── index.js ├── images/ ├── db/ │ └── db.js └── routes/ └── image_routes.js └── controllers/ └── upload.js</code>
-
PostgreSQL ?????? ? ??? ??? ??:
CREATE DATABASE uploader; CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, icon VARCHAR NOT NULL);
?? ??:
-
?????? ??(db.js):
const { Client } = require('pg'); const client = new Client({ user: 'postgres', host: 'localhost', database: 'uploader', password: 'YOUR_PASSWORD', // 請?zhí)鎿Q為您的數(shù)據(jù)庫密碼 port: 5432, }); client.connect().then(() => console.log("Connected to DB")).catch(err => console.error("數(shù)據(jù)庫連接失敗:", err.message)); module.exports = client;
-
????? ??(index.js):
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const port = process.env.PORT || 4000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use('/images', express.static('images')); // 設置靜態(tài)文件目錄 require('./routes/image_routes')(app); app.listen(port, () => { console.log(`服務器運行在端口 ${port}`); });
-
?? ??(image_routes.js):
const express = require('express'); const uploadController = require('../controllers/upload'); module.exports = (app) => { app.post('/upload/single', uploadController.upload.single('icon'), uploadController.uploadSingleImage); app.post('/upload/multiple', uploadController.upload.array('icon', 12), uploadController.uploadMultipleImage); };
-
??? ??? ????(upload.js):
const multer = require('multer'); const path = require('path'); const client = require('../db/db'); const storage = multer.diskStorage({ destination: (req, file, cb) => cb(null, './images'), filename: (req, file, cb) => cb(null, `image-${Date.now()}${path.extname(file.originalname)}`), }); const fileFilter = (req, file, cb) => { if (!file.originalname.match(/\.(jpg|jpeg|png)$/i)) { return cb(new Error('請上傳 JPG, JPEG 或 PNG 格式的圖片'), false); } cb(null, true); }; exports.upload = multer({ storage, fileFilter }); exports.uploadSingleImage = async (req, res) => { try { await client.query(`INSERT INTO users (name, icon) VALUES ('${req.body.name}', '${req.file.filename}')`); res.json({ statusCode: 200, status: true, message: '圖片上傳成功' }); } catch (error) { console.error("圖片上傳失敗:", error); res.status(500).json({ statusCode: 500, status: false, message: '圖片上傳失敗' }); } }; exports.uploadMultipleImage = async (req, res) => { try { for (const file of req.files) { await client.query(`INSERT INTO users (name, icon) VALUES ('${req.body.name}', '${file.filename}')`); } res.json({ statusCode: 200, status: true, message: '所有圖片上傳成功' }); } catch (error) { console.error("圖片上傳失敗:", error); res.status(500).json({ statusCode: 500, status: false, message: '圖片上傳失敗' }); } };
???:
Postman? ???? ??? ??? ??? ?? ???? ???? /upload/single
?? /upload/multiple
? POST ??? ????.
? ??? YOUR_PASSWORD
? PostgreSQL ?????? ????? ??? ??? ?? ?????. ? ??? ?? ???? ???? ?? ?? ??? ???????. ??? ?? ??? ?? ???????. ????? ??? ?? images
? ???? ? ??? ???? ?? ?? ???.
? ??? ??? ?? ?? ? ??? ???? ???? ?? ???? ??? ???? ?????. YOUR_PASSWORD
? ?? ?? ???? ?? ?? ???? ?????.
? ??? multer (postgresql)? ???? ???? ????? ?? Nodejs Express REST API? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











Java ? JavaScript? ?? ?? ????? ??? ?? ?? ?? ???? ????? ?????. Java? ??? ? ??? ?????? ??? ???? JavaScript? ?? ? ??? ??? ?????.

JavaScriptCommentsareEnsentialformaining, ?? ? ???? 1) Single-LinecommentsERUSEDFORQUICKEXPLANATIONS.2) Multi-linecommentSexplaincleClexLogicOrprovidedEdeDDocumentation.3) inlineecommentsClarifySpecificPartSofcode.bestPractic

JavaScript?? ??? ??? ?? ? ? ?? ??? ???????. 1. ?? ??? ??? ???? ?? ??? ????. ISO ?? ???? ???? ???? ???? ?? ????. 2. ?? ??? ?? ???? ??? ?? ???? ??? ? ??? ? ?? 0?? ????? ?? ??????. 3. ?? ?? ???? ???? ???? ?? ?????? ??? ? ????. 4. Luxon? ?? ???? ???? ?????? ???? ?? ????. ??? ?? ???? ????? ???? ??? ????? ?? ? ????.

TAGGSATTHEBOTTOMOFABLOGPOSTORWEBPAGESERVESPRACTICALPURSEO, USEREXPERIENCE, andDESIGN.1.ITHELPSWITHEOBYOWNSESPORENGENSTOESTOCESKESKERKESKERKERKERDER-RELEVANTTAGSWITHOUTHINGTEMAINCONTENT.2.ITIMPROVESEREXPERKEEPINGTOPONTEFOCUSOFOFOFOCUSOFOFOFOCUCUSONTHEATECLL

JavaScriptIspreferredforwebDevelopment, whithjavaisbetterforlarge-scalebackendsystemsandandandoidapps.1) javascriptexcelsincreatinginteractivewebexperiences withitsdynatureanddommanipulation.2) javaoffersstrongtypingandobject-Orientededededededededededededededededdec

javascriptassevenfundamentalDatatatypes : ??, ???, ??, unull, ??, ? symbol.1) ?? seAdouble-precisionformat, ??? forwidevaluerangesbutbecautiouswithfatingfointarithmetic.2) stringsareimmutable, useefficientconcatenationmethendsf

??? ?? ? ??? DOM?? ??? ??? ? ?????. ??? ?? ????? ?? ??????, ??? ?? ???? ?? ????????. 1. ??? ??? addeventListener? usecapture ?? ??? true? ???? ?????. 2. ??? ??? ?? ???? usecapture? ???? ????? ?????. 3. ??? ??? ??? ??? ???? ? ??? ? ????. 4. ??? ?? ?? ?? ??? ?? ??? ??????? ??? ???? ?????. 5. ??? ?? ?? ?? ??? ?? ???? ?? ???? ? ??? ? ????. ? ? ??? ???? ???? JavaScript? ??? ??? ??? ????? ???? ???? ??? ??????.

Java? JavaScript? ?? ????? ?????. 1. Java? ???? ???? ??? ? ??? ?????? ?????? ? ?? ???? ?????. 2. JavaScript? ?? ? ?? ?? ? ??? ?? ??? ???? ??? ? ?? ? ?? ?????.
