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

@bucky24/node-php

v0.7.15

Published

Allows running a PHP server via node.

Downloads

86

Readme

@bucky24/node-php

Allows running a PHP server via node.

This is basically the same thing as https://www.npmjs.com/package/node-php and https://www.npmjs.com/package/@windastella/php-server (which is basically what node-php is using under the hood), however those packages are dependent on express and will only work with express servers.

Why would you ever want to do this? You're a JavaScript programmer, not a PHP programmer! Well the fact remains that few webhosts offer node hosting. Most of them offer PHP hosting, meaning if you want to have server code, you're probably going to need to write it in PHP for most basic sites.

But then why node? Well because you're a JavaScript programmer, and you're probably writing your frontend in JavaScript. You might not want to install a full LAMP or WAMP stack. Apache does a number of powerful things but can be overkill for simple backends. (though given that you need PHP and probably have MySQL installed, we're basically building a LNMP or WNMP stack). This module gives you the ability to spin up an extremely lightweight web server that knows how to call PHP scripts to run those extremely simple backends.

In all honesty, like most of my modules, if you want to do the things I describe above, you should probably use one of the modules listed above, as they will be better and more feature-full than what I am building here. I built this for a number of reasons:

  1. I like building things.
  2. I want something that doesn't require express as a dependency.

You are probably using express, so it would be much easier in your case to just use node-php and be done with it.

If you are still here and have decided to move forward, then you probably will be interested in how to use this library:

Usage

This module exports a single function. Under the hood, it's called serve, but since its a default export, you can call it whatever you like.

serve

The serve method takes in the following parameters:

| Param | Type | Description | |---|---|---| | mainDirectory | File Path as String | The directory of the main php code. Required | | port | Integer | The port to start the http server on. Required | | staticDirectory | File Path as String | The directory where static files are to be found. Optional | | phpDirectory | File Path as String | The directory where the php-cgi binary is located. You can set this to override where the module looks for the php binary. Optional |

Example:

const serve = require("@bucky24/node-php");

serve(__dirname, 80);

Server Capabilties and Limitations

Limitations

This module is extremely feature-lean. To that effect, there are the following limitations:

  • Not all request bodies are recognized. The system can recognize the following types:
    • application/json
    • application/x-www-form-urlencoded
    • multipart/form-data (this includes nested form data)
  • Any parameters in the query string will be overwritten in the $_REQUEST object by any duplicated keys in the request body. I'm not completely sure how PHP/Apache handles this normally.
  • For multipart data, the system will handle files sent. It should be able to handle any files. It uses the filename to determine the extension for the new file.
  • The server will properly pass through any headers set by the php server to the client (into the $_SERVER variable). Headers may not be correctly populated from the client.
  • The server might not be able to write binary data in some cases (any Content-Type set to an image or video should be supported, as well as PDFs)

The module only sets the following properties:

  • $_REQUEST - all valid values
  • $_GET - all valid values
  • $_POST - all valid values
  • $_SERVER
    • REQUEST_URI
    • DOCUMENT_ROOT
    • HTTP_HOST
    • SERVER_NAME (set to same value as HTTP_HOST, this may not be correct in all circumstances)
    • HTTPS (set to 'off', this may not be correct)
    • REQUEST_METHOD
  • $_SESSION - This is handled automatically by PHP, but does work correctly with this module.
  • $_COOKIE - contains raw cookie data

.htaccess

The module provides extremely limited .htaccess parsing:

  • Only handles .htaccess in the starting directory
  • Only handles mod_rewrite.c
  • Only handles simple URL rewriting using RewriteRule
  • Does not handle RewriteCond
  • All existing parameters are appended to the new URL (the [QSA] flag is on by default)
  • Handles the [R] flag correctly
  • Any other flag given is currently ignored

Other Capabilties

The server will attempt to serve static files from the staticDirectory, using the mainDirectory as a backup.

  • Currently only html, js, and css files will be sent back with the appropriate Content-Type set
  • The server will attempt to load an index.html file from first the staticDirectory then the mainDirectory if it can't find an index.php in the mainDirectory