Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/c0d27a0e6a6d2313d51b30528708fe55 to your computer and use it in GitHub Desktop.
Save code-boxx/c0d27a0e6a6d2313d51b30528708fe55 to your computer and use it in GitHub Desktop.
NodeJS Express Image Gallery
// (A) LOAD REQUIRED MODULES
const express = require("express"),
path = require("path"),
glob = require("glob");
// (B) INIT EXPRESS SERVER
const app = express();
app.set("view engine", "ejs");
// (C) GET IMAGES
var images = glob.sync("gallery/**/*.{jpg,jpeg,png,webp,gif}");
var names = [];
images.forEach(img => names.push(path.parse(img).name));
// (D) GALLERY IMAGES & PAGE
app.use("/public", express.static(path.join(__dirname, "public")))
app.use("/gallery", express.static(path.join(__dirname, "gallery")))
app.get("/", (req, res) => res.render("2a-gallery.ejs" , { images: images, names: names }));
// (E) START SERVER
app.listen(80, () => console.log(`Server running at port 80`));
md gallery
md public
md views
move 2a-gallery.ejs views
move 2b-gallery.css public
move 2c-gallery.js public
curl https://user-images.githubusercontent.com/11156244/281620198-f6d73a31-c48b-4599-afc2-ea2645cc1ffd.png --ssl-no-revoke --output gallery/Wind.png
curl https://user-images.githubusercontent.com/11156244/281620196-8bf995b2-d700-4713-9d7b-5d5d7489fc26.png --ssl-no-revoke --output gallery/Water.png
curl https://user-images.githubusercontent.com/11156244/281620193-1821c7c0-9afd-4da3-8c0c-590a7933d3c9.png --ssl-no-revoke --output gallery/Fire.png
curl https://user-images.githubusercontent.com/11156244/281620187-1ea3a8c0-6da2-417c-be24-be54570b9b3d.png --ssl-no-revoke --output gallery/Earth.png
call npm i glob express ejs
node 1-server.js
mkdir -m 777 gallery
mkdir -m 777 public
mkdir -m 777 views
mv ./2a-gallery.ejs ./views
mv ./2b-gallery.css ./public
mv ./2c-gallery.js ./public
curl https://user-images.githubusercontent.com/11156244/281620198-f6d73a31-c48b-4599-afc2-ea2645cc1ffd.png --ssl-no-revoke --output ./gallery/Wind.png
curl https://user-images.githubusercontent.com/11156244/281620196-8bf995b2-d700-4713-9d7b-5d5d7489fc26.png --ssl-no-revoke --output ./gallery/Water.png
curl https://user-images.githubusercontent.com/11156244/281620193-1821c7c0-9afd-4da3-8c0c-590a7933d3c9.png --ssl-no-revoke --output ./gallery/Fire.png
curl https://user-images.githubusercontent.com/11156244/281620187-1ea3a8c0-6da2-417c-be24-be54570b9b3d.png --ssl-no-revoke --output ./gallery/Earth.png
source "npm i glob express ejs"
node 1-server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment