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

ojparty

v3.0.5

Published

OJParty is a lightweight Node.js framework that enhances the default HTTP module by offering Express-like routing with added support for file uploads and session management, making it easy to build web applications without heavy dependencies.

Downloads

571

Readme

OJParty Framework

OJParty is a lightweight Node.js framework designed to provide developers with a straightforward and powerful way to build web applications. With support for dynamic templating, session management, file uploads, and database interactions, OJParty streamlines the process of developing modern web applications.

Features

  • Dynamic Templating: Embed JavaScript directly within .ojp files for powerful, server-side rendering.
  • Session Management: Easily manage user sessions with intuitive methods.
  • File Uploads: Handle file uploads with minimal configuration.
  • MySQL Database Integration: Interact with MySQL databases seamlessly.
  • Static File Serving: Serve static files easily with configurable settings.
  • Custom 404 Page: Define a custom 404 error page to enhance user experience.

Installation

npm install ojparty

Quick Start

Here's a simple example to get you started:

const ojp = require("ojparty");

const app = ojp.ojparty.app();

app.settings({
    serveStatic: true, // Enable static file serving
    404: '404.html'    // Set default 404 page
});

app.get("/", (req, res) => {
    res.ojp('index.html');
    res.end();
});

app.listen(8080);

Basic Usage

Here’s how to create a simple server with OJParty, featuring session management, file upload capabilities, and more.

Example 1: Handling Sessions and File Uploads

const ojp = require("ojparty");
const app = ojp.ojparty.app();

app.get("/", (req, res) => {
  req.setSession('username', req.query.name);
  res.sendFile('index.html');
});

app.get("/profile", (req, res) => {
  res.send(`Welcome to your profile page, ${req.session.username}`);
});

app.post("/uploadfile", (req, res) => {
  res.send(JSON.stringify(req.files));
});

app.post("/unset-session", (req, res) => {
  req.unsetSession('username');
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.send('Your session has been unset');
});

app.listen(210, () => {
  console.log('Server is running on port 210');
});

Example 2: Parsing Forms and Handling File Uploads

const ojp = require("ojparty");
const http = require("http");
const form = ojp.ojparty.forms;

const serve = http.createServer((req, res) => {
  if (req.method == "POST") {
    form(req, (data) => {
      res.setHeader('Content-Type', 'text/plain');
      res.statusCode = 200;
      res.write(Buffer.from(JSON.stringify(data)));
      res.end();
    });
  }
  if (req.method == "GET") {
    const fs = require('fs');
    try {
      const data = fs.readFileSync('form.html');
      res.setHeader('Content-Type', 'text/html');
      res.statusCode = 200;
      res.write(Buffer.from(data));
      res.end();
    } catch (e) {
      res.setHeader('Content-Type', 'text/html');
      res.statusCode = 500;
      res.write(Buffer.from(e.toString()));
      res.end();
    }
  }
});

serve.listen(302, () => {
  console.log('Server is running on port 302');
});

Example 3: File Upload with Utilities

const ojp = require("ojparty");
const http = require("http");
const form = ojp.ojparty.forms;
const util = ojp.ojparty.util;

const serve = http.createServer((req, res) => {
  if (req.method == "POST") {
    form(req, (data) => {
      res.setHeader('Content-Type', 'text/plain');
      res.statusCode = 200;
      for (let i = 0; i < data.files.length; i++) {
        util.writeFile(`uploads/${data.files[i].fileName}`, data.files[i].file.data);
        res.write(`${data.files[i].fileName} has been written to /uploads/${data.files[i].fileName}. `);
      }
      res.end();
    });
  }
  if (req.method == "GET") {
    const fs = require('fs');
    try {
      const data = fs.readFileSync('uploadform.html');
      res.setHeader('Content-Type', 'text/html');
      res.statusCode = 200;
      res.write(Buffer.from(data));
      res.end();
    } catch (e) {
      res.setHeader('Content-Type', 'text/html');
      res.statusCode = 500;
      res.write(Buffer.from(e.toString()));
      res.end();
    }
  }
});

serve.listen(303, () => {
  console.log('Server is running on port 303');
});

Templating with OJP Files

OJParty allows you to embed JavaScript within your .ojp files using the special tags $[ojp] and $[/ojp]. You can also access variables passed from the app functions directly within the .ojp file.

Example

app.get("/profile", (req, res) => {
    const userProfile = {
        name: "Samuel",
        age: 23,
        hobbies: ["swimming", "cooking"]
    };
    res.ojp('profile.ojp', userProfile);
    res.end();
});

profile.ojp

<h1>Welcome, $[name]</h1>
<p>Age: $[age]</p>
<p>Hobbies:</p>
<ul>
$[ojp]
var hobbies = $[hobbies];
hobbies.forEach(function(hobby) {
    sam(`<li>${hobby}</li>`);
});
$[/ojp]
</ul>

Session Management

app.get("/login", (req, res) => {
    req.setSession('username', 'Samuel');
    res.ojp('dashboard.ojp');
    res.end();
});

app.get("/dashboard", (req, res) => {
    if (req.session.username) {
        res.ojp('dashboard.ojp', { username: req.session.username });
    } else {
        res.setHeader('location', '/login');
        res.statusCode = 302;
    }
    res.end();
});

Handling File Uploads


app.post("/upload", (req, res) => {
    const uploadedFiles = req.files;
    res.ojp('upload-result.ojp', { files: uploadedFiles });
    res.end();
});

Database Interaction

const mysql = require("mysql");
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "ojp"
});

app.post("/register", (req, res) => {
    const { fullname, username, email, password } = req.body;
    const userId = ojp.util.random('mix', 24);
    connection.query(
        "INSERT INTO users (userId, fullname, username, email, password) VALUES (?, ?, ?, ?, ?)",
        [userId, fullname, username, email, password],
        (err) => {
            if (err) throw err;
            req.setSession('userId', userId);
            res.ojp('welcome.ojp');
            res.end();
        }
    );
});

Configuration with app.settings()

You can customize your application settings using app.settings():

app.settings({
    serveStatic: true,  // Enables serving of static files
    404: '404.html'     // Specifies the default 404 error page
});
  • ** serveStatic: ** Set to true or false to enable or disable serving static files.
  • ** 404: ** Specify the file path for the custom 404 error page.

Contributing

If you'd like to contribute to the OJParty framework, please fork the repository and submit a pull request. We welcome contributions from the community!