Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

File Exposer

A lightweight, secure HTTP file server for exposing files from a local directory via HTTP requests.

Overview

File Exposer is a simple Node.js-based HTTP server that allows you to serve files from the server's directory through HTTP GET requests. It's designed for quick file sharing and testing scenarios where you need to make local files accessible via HTTP.

Features

  • 🚀 Simple Setup - No configuration required, just run and go
  • 🔒 Security Built-in - Directory traversal protection to prevent unauthorized access
  • 📁 Multiple MIME Types - Supports common file formats with proper content-type headers
  • Lightweight - Uses only Node.js built-in modules, no external dependencies
  • 🎯 RESTful API - Clean URL structure for accessing files

Supported File Types

The server automatically detects and serves files with appropriate MIME types:

  • Web: .html, .htm, .css, .js, .aspx
  • Data: .json, .xml, .txt
  • Images: .png, .jpg, .jpeg, .gif, .svg, .ico
  • Documents: .pdf, .zip
  • Fallback: Any other file type is served as application/octet-stream

Installation

  1. Clone or download this repository
  2. Ensure you have Node.js installed (no additional packages required)
# No npm install needed - uses only Node.js built-in modules!

Usage

Starting the Server

node expose.js

The server will start on http://localhost:4567 by default.

Accessing Files

To access a file, simply append the filename to the server URL:

http://localhost:4567/{filename}

Examples:

# Access an HTML file
http://localhost:4567/index.html

# Access an image
http://localhost:4567/logo.png

# Access a JSON file
http://localhost:4567/data.json

# Access files in subdirectories
http://localhost:4567/assets/style.css

API Reference

GET /{filename}

Retrieves the requested file from the server directory.

Parameters:

  • filename (path parameter) - The name/path of the file relative to the server directory

Responses:

Status Code Description
200 File found and returned successfully
400 No file specified or attempting to read a directory
403 Access denied (directory traversal attempt detected)
404 File not found
500 Internal server error

Success Response (200):

Content-Type: {appropriate MIME type}
{file contents}

Error Response Examples:

// 400 - No file specified
{
  "error": "No file specified",
  "usage": "GET /{filename} - Returns the requested file from the server directory"
}

// 403 - Directory traversal attempt
{
  "error": "Access denied: Directory traversal not allowed"
}

// 404 - File not found
{
  "error": "File not found",
  "path": "nonexistent.txt"
}

Security Features

Directory Traversal Protection

The server implements multiple layers of protection against directory traversal attacks:

  1. Pattern Detection - Blocks requests containing .., \.., or ../
  2. Path Validation - Ensures the resolved file path stays within the server directory
  3. Normalized Path Checking - Uses Node.js path.join() to prevent path manipulation

Blocked Examples:

# These will all return 403 Forbidden
http://localhost:4567/../etc/passwd
http://localhost:4567/../../secret.txt
http://localhost:4567/subfolder/../../config.json

Configuration

You can modify the following constants in expose.js to customize the server:

const PORT = 4567;        // Server port
const HOST = 'localhost'; // Server host

Use Cases

  • Local Development - Serve static files during development
  • Quick File Sharing - Share files on a local network
  • Testing - Test file downloads and MIME type handling
  • Prototyping - Quickly expose files for web applications
  • SharePoint Development - Serve local files for SharePoint customization testing

Limitations

  • Single Directory - Only serves files from the directory where expose.js is located
  • No Directory Listing - Cannot browse directories, must specify exact file paths
  • No Authentication - No built-in user authentication or authorization
  • Local Network Only - Designed for localhost/local network use, not production

Error Handling

The server provides detailed error messages for common scenarios:

  • Missing File - Returns 404 with the requested path
  • Directory Access - Returns 400 when trying to read a directory
  • Security Violations - Returns 403 for directory traversal attempts
  • Server Errors - Returns 500 with error details for unexpected issues

Example Workflow

  1. Place expose.js in a directory with files you want to serve
  2. Start the server: node expose.js
  3. Access files via browser or HTTP client:
    curl http://localhost:4567/myfile.txt

Troubleshooting

Server won't start:

  • Check if port 4567 is already in use
  • Ensure Node.js is properly installed

File not found:

  • Verify the file exists in the same directory as expose.js
  • Check the file path (case-sensitive on some systems)
  • Ensure you're not trying to access a directory

403 Forbidden:

  • Remove any .. or path traversal characters from your request
  • Ensure the file is within the server directory

License

This project is provided as-is for personal and educational use.

Contributing

Feel free to fork and modify this server for your specific needs. Common enhancements include:

  • Adding authentication
  • Supporting directory listing
  • Implementing file upload
  • Adding CORS headers
  • Supporting HTTPS

Note: This server is intended for development and testing purposes. For production use, consider using established solutions like Express.js, Nginx, or Apache.

About

A lightweight, zero-dependency Node.js HTTP server for quickly exposing local files via HTTP. Features built-in security against directory traversal and automatic MIME type detection. Perfect for development, testing, and quick file sharing.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages