express-pg-posty

1.0.1 • Public • Published

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 'file=@test.txt' http://localhost:3000/posty

Readme

Keywords

none

Package Sidebar

Install

npm i express-pg-posty

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

78.5 kB

Total Files

7

Last publish

Collaborators

  • seveibar