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

express-pg-posty

v1.0.1

Published

A simple express file upload and serve middleware using postgres.

Downloads

3

Readme

express-pg-posty

A simple express file upload and serve middleware using postgres.

Usage

const express = require("express")
const pgPosty = require("express-pg-posty")

const app = express()

app.use(
  "/posty",
  pgPosty({
    pgCredentials: {
      host: "127.0.0.1",
      user: "your_database_user",
      password: "your_database_password",
      database: "myapp_test"
    }
  })
)

You can now POST files to /posty. Each file will be given a unique id and can be retrieved by GETing /posty/<file_id>

Advanced Usage

const express = require("express")
const pgPosty = require("express-pg-posty")

const app = express()

app.use(
  pgPosty({
    // Required: Credentials to Postgres Database
    pgCredentials: {
      host: "127.0.0.1",
      user: "your_database_user",
      password: "your_database_password",
      database: "myapp_test"
    },

    // Optional: Specify table name
    tableName: "posty_file",

    // Optional: Return true to authorize a file upload
    authorizePOST: (req, file) => true,

    // Optional: Return true to authorize a file request
    // `info` contains the row data for the file (app_data, data, created_at etc.)
    authorizeGET: (req, info) => true
  })
)

Endpoints

Posty will create the following endpoints. Note that the prefix /posty is based on the example app.use("/posty", pgPosty({/* ... */})).

| Endpoint | Description | Parameters | | ----------------------- | -------------------------------------------------------------------------- | -------------------------------------- | | POST /posty | Upload a file | Supply file as a multipart/form file | | GET /posty/:file_id.* | Returns the file uploaded to posty. The extension is optional and ignored. | |

The POST request will return the following response:

{
  success: true,
  file_id: "<uuid>",
  path: "/posty/<uuid>",
  url: "https://yourwebsite.com/posty/<uuid>"
}

Database Table

A table (by default "posty_file") will be created to store all of your files. This is the structure of the table:

| column_name | data_type | is_nullable | column_default | comment | | ----------- | ------------------------ | ----------- | -------------- | ------- | | file_id | uuid | no | | | | data | bytea | no | | | | mimetype | text | no | | | | filename | text | yes | | | | created_at | timestamp with time zone | no | now() | | | app_data | jsonb | yes | | |

FAQ

Isn't it bad to store files in a database?

It depends. Storing files in a database often makes local development easier and the retrieval performance penalty is negligible for a lot of applications. If you're looking for high performance reads, consider using S3.

Won't this allow anyone to upload files to my site? What about rate limiting etc.

This middleware provides the file upload/serving capabilities, you'll want to combine it with other middleware to validate requests, provide rate limiting etc.

I'd like to provide other data alongside the file, how do I do that?

You can set req.postyAppData to whatever you want prior to posty processing the upload, it'll be stored alongside the file in the database in the app_data column.

How do I upload files?

Depends on your framework/library. Use a regular POST request with a multipart/form upload.

You can test that posty is working with a quick curl request:

curl -F '[email protected]' http://localhost:3000/posty