This is a lightweight, self-hosted image server built with Node.js and Express. It allows you to upload images via an API and manage them through a simple web interface. All core logic is contained within this single repository, making it easy to deploy and maintain.
- Upload API: Securely upload images via a
POST /uploadendpoint. - Web Interface: Browse, view, and manage your uploaded images through a web-accessible list.
- Thumbnail Generation: Automatically creates thumbnails for uploaded images.
- Authentication: Basic authentication protects your upload and image list endpoints.
- HTTPS Support: Ready for production deployment with SSL/TLS encryption.
- Lazy Loading: Efficiently loads thumbnails on the image list page for better performance.
- Node.js (version 14 or higher recommended)
- Yarn or npm
Clone the repository and install the necessary dependencies.
git clone <repository-url>
cd <repository-directory>
yarn install
# or
npm installNote on sharp: The sharp library is used for image processing. If you encounter network issues during installation, configure npm/yarn to use a mirror:
npm config set sharp_binary_host "https://npmmirror.com/mirrors/sharp"
npm config set sharp_libvips_binary_host "https://npmmirror.com/mirrors/sharp-libvips"
npm install
# or for yarn
yarn config set sharp_binary_host "https://npmmirror.com/mirrors/sharp"
yarn config set sharp_libvips_binary_host "https://npmmirror.com/mirrors/sharp-libvips"
yarn installThe application uses environment variables for configuration. Create a .env file in the project root to set the following variables:
PORT: (Optional) The port the server will listen on. Defaults to3999.SECURE_USERNAME: (Required) The username for basic authentication.SECURE_TOKEN: (Required) The password/token for basic authentication.
Example .env file:
PORT=3999
SECURE_USERNAME=myuser
SECURE_TOKEN=mysecretpasswordFor development and debugging, use the built-in nodemon for auto-reloading:
yarn dev
# or
npm run devFor production deployment:
- SSL Certificates: Place your SSL certificate (
img.pem) and private key (img.key) in a folder namedkeyswithin the project root. - Process Manager (Recommended): Use a process manager like
pm2to keep the application running.pm2 start npm --name "self-pic" -- run start - Direct Start: Alternatively, you can start the server directly:
yarn start # or npm run start
Once the server is running:
- Accessing the Image List: Navigate to
https://your-domain.com/in your browser. You will be prompted for the username and password you configured. This page lists all uploaded images. - Uploading an Image: Use a tool like
curlor a programming script to send aPOSTrequest tohttps://your-domain.com/upload. Example usingcurl:A successful upload will return a JSON response with the URL of the uploaded image.curl -u myuser:mysecretpassword -F "image=@/path/to/your/image.jpg" https://your-domain.com/upload - Viewing an Image: Click the filename link on the image list page, or directly access the image via its URL:
https://your-domain.com/image/filename.jpg.
index.js: Main application entry point.uploader.js: Handles image uploading and thumbnail generation.downloader.js: Serves images and generates the image list HTML page.middlewares.js: Contains Express middleware for CORS, logging, authentication, etc.const.js: Defines constants and loads environment variables.logger.js: A simple logging utility.public/: Contains static assets like the favicon and the image list HTML template.uploads/: (Created on first upload) The directory where uploaded images are stored.uploads/thumbnails/: (Created on first upload) The directory where generated thumbnails are stored.
- Automatic
uploads/keys/logsfolder creation - Upload multiple images at once
- Uploading folder backup
- Maybe add a standalone upload button in image_list page