HacMuHai / Hiep_cnm_lab04_mvc

0 stars 0 forks source link

install #2

Open HacMuHai opened 7 months ago

HacMuHai commented 7 months ago

HuuHiep.txt

HacMuHai commented 7 months ago

id:339712995588 name:HuuHiep HuuHiep123 accessKey:AKIAU6GDY6ECDKSPBJNE secretKey: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

----install npm i express multer nodemon ejs dotenv body-parser aws-sdk

--env ACCESS_KEY_ID=AKIAU6GDY6ECDKSPBJNE SECRET_ACCESS_KEY=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 REGION=ap-southeast-1 DYNAMODB_TABLE_NAME=HuuHiepCourse S3_BUCKET_NAME=huuhiepgk

--server const express = require("express"); const multer = require("multer"); const path = require("path"); const AWS = require("aws-sdk"); const { v4: uuidv4 } = require("uuid"); // const courses = require("./datas/courses.js"); require("dotenv").config();

const PORT = 3000; const app = express();

// register middleware app.use(express.json({ extended: false })); app.use(express.static("./views"));

// config view engine app.set("view engine", "ejs"); app.set("views", "./views");

// cấu hình multer const storage = multer.memoryStorage({ destination: function (req, file, callback) { callback(null, ""); }, });

//cấu hình aws process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = "1";

//cấu hình IAMUser AWS.config.update({ accessKeyId: process.env.ACCESS_KEY_ID, secretAccessKey: process.env.SECRET_ACCESS_KEY, region: process.env.REGION, });

const s3 = new AWS.S3(); const dynamoDB = new AWS.DynamoDB.DocumentClient(); const bucketName = process.env.S3_BUCKET_NAME; const tableName = process.env.DYNAMODB_TABLE_NAME;

function checkFileType(file, callback) { const filetypes = /jpeg|jpg|png|gif/; const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = filetypes.test(file.mimetype); if (extname && mimetype) { return callback(null, true); } else { return callback("Error: Images Only!"); } }

const upload = multer({ storage, limits: { fileSize: 3000000, // giới hạn file 5MB }, fileFilter(req, file, callback) { checkFileType(file, callback); }, });

app.listen(PORT, () => { console.log(Sever is running on port ${PORT}); });

app.get("/", (req, resp) => { try { const params = { TableName: tableName }; dynamoDB.scan(params, (err, data) => { if (err) { console.log("error Scan", err); return resp.send("Error Scan"); } else { // console.log("data", data.Items); return resp.render("index.ejs", { data: data.Items }); } }); } catch (err) { console.log("error", err); return resp.status(500).send("Internal Server Error Scan"); } });