The purpose of the project is to create a web server that supports multiple domains, a ban system, and logging capabilities.
Project Utility
This project is useful for serving files over HTTP with support for multiple domains, banning IP addresses, and logging requests.
Project Description
The file server is implemented using Node.js and allows configuration for multiple domains, MIME types, and logging paths. It also supports banning specific IP addresses.
Repository and Installation
Repository: File Server on GitHub
Installation
- Clone the repository:
Terminal window git clone https://github.com/Fulldroper/file-server.git - Install the dependencies:
Terminal window npm install
Setup
Edit the settings.json
file to configure the server:
{ "serverPort":"1337", "homeDir":"home", "logsPath":"./", "hosts":["localhost","my.new.domen.com","192.168.0.1"], "types":{ ".js":"text/javascript", ".txt":"text/plain", ".css":"text/css", ".json":"application/json", ".png":"image/png", ".jpg":"image/jpg", ".ico":"image/x-icon", ".wav":"audio/wav", ".mp4":"video/mp4", ".zip":"application/zip", ".rar":"application/x-rar-compressed", ".iso":"application/octet-stream" }}
Blocking an IP
Add the IP to banlist.json
:
[ "192.168.0.1", "192.168.0.2"]
Adding a Domain
- Add the domain to the
hosts
array insettings.json
. - Create a folder named after the domain inside the
homeDir
.
Algorithm
- Server Initialization:
const http = require('http');const settings = require('./settings.json');// Initialize server with settings
- Request Handling:
http.createServer((req, res) => {// Handle request based on domain and file type}).listen(settings.serverPort);
- Logging:
const fs = require('fs');// Log request detailsfs.appendFile('log.txt', logData, (err) => {if (err) throw err;});
- Ban System:
const banlist = require('./banlist.json');// Check if the requester's IP is in the banlistif (banlist.includes(req.connection.remoteAddress)) {res.writeHead(403);res.end('Access denied');}
Skills Acquired
- Understanding of HTTP servers with Node.js
- Implementing multi-domain support
- Managing and processing logs
- IP banning mechanisms