npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

imagic-web-server

v1.0.3

Published

Easy web-server

Downloads

7

Readme

imagic-web-server

imagic-web-server is a lightweight and flexible Node.js web server module that provides a simple and extensible framework for serving web content and handling HTTP requests. It supports both HTTP and HTTPS, allows you to define custom routes and middlewares, and provides utilities for serving static assets. Below, you will find a comprehensive guide on how to use imagic-web-server with code examples for each of its features.

Installation

You can install the imagic-web-server module using npm or yarn:

npm install imagic-web-server

Getting Started

Here's how to create a basic web server instance using imagic-web-server:

import WebServer from 'imagic-web-server';

// Initialize a new HTTP server
const server = new WebServer({
https: false, // Set to true for HTTPS
// Other server options (e.g., port, hostname) can be added here
assets: [
// Define your static assets configuration here (optional)
// Example: { route: '/static', dir: 'public' }
],
});

// Start the server on the specified port and hostname

To create an HTTP server, initialize a new WebServer instance with the desired options. You can specify whether you want to use HTTPS, set server options such as the port and hostname, and define static assets. You can add assets using the assets property, which is an array of asset configurations.

Handling Requests

imagic-web-server provides a requestListener method to handle incoming HTTP requests. This method allows you to process requests and define routes and middlewares.

server.requestListener = (req, res) => {
  // Request handling logic here
};

n the requestListener, you can access the req (request) and res (response) objects to handle the incoming request and generate the response.

Adding Custom Routes

You can create custom routes using the createRoute method. A route is defined by an HTTP method, URL, and a callback function.

server.createRoute({ methods: ['GET'], url: '/my-route' }, (req, res) => {
  // Handle the GET request for '/my-route'
});

In the example above, we create a custom route that listens for GET requests at the /my-route URL.

Adding Middlewares

Middlewares are functions that can intercept and process requests before they reach the route handler. You can use the use method to add middlewares.

server.use((req, res, next) => {
  // Middleware logic here
  next();
});

The next function should be called to pass control to the next middleware or the route handler.

Starting the Server

After configuring your server, you can start it using the listen method.

server.listen(8080, 'localhost', () => {
  console.log('Server is running on port 8080');
});

In this example, the server listens on port 8080 and the 'localhost' hostname. You can change these values as needed.

Responding to Requests

Inside the requestListener, you can use various response methods to send data back to the client. Here are some commonly used methods:

Redirect

res.redirect(301, '/new-location');

Set HTTP Status Code

res.status(200);

Send JSON

res.json({ message: 'Hello, World!' });

Handling Static Assets

imagic-web-server allows you to serve static assets, such as HTML, CSS, and JavaScript files. Simply add the asset configurations when creating the WebServer instance.

assets: [
  { route: '/static', dir: 'public' },
]

In this example, any request to /static will be served from the public directory. If the requested asset exists, it will be served. If not, a 404 response will be sent. Feel free to customize the asset configuration to match your project's structure.

Conclusion

imagic-web-server is a versatile and easy-to-use web server module for Node.js. It enables you to create a custom HTTP server with routes, middlewares, and static asset serving. Use the provided examples and adapt them to your specific needs to build powerful web applications.