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

serve-my-app

v0.3.0

Published

Deploy your javascript application and add your own routes in a minute.

Downloads

15

Readme

ServeMyApp

npm npm

🚀 Serve your Javascript application and/or add an API to it in a minute!

This package allows you to add a NodeJS Express server easily and quickly to your Javascript application.

screenshot

✨ Features:

Includes a fully customizable Express Server, which allows you to:

  • Serve your Javascript application, with routing handling.
  • Add directly your api routes into your project, without thinking to something else.

Table of contents


Getting started

In this ReadMe, we will take an example with a create-react-app application, and a @vue/cli one is coming. But it can be what you want, so please, let us know if you tested it with another framework or app. It will be added to this doc.

Installation

cd my-app
npm install serve-my-app express

Automatic initialisation

npx sma-init

Configuration

The serve-my-app binary will serve your frontend and your api.

It has several configuration parameters.

See usage section to understand how to use them.

Most useful ones:

| Option | Type | Default value | Usage | |--------- |----------------|---------------|-----------------------------------------------------------------------------------| | --srv | string / false | false | Serve api routes from this folder (relative path) | | --src | string / false | false | Serve your application from this folder (browser compatible files) | | --watch | false | false | Restart server if change detected in --srv folder | | --proxify | boolean | false | Proxify calls from your app to the express server (depends of you app dev server) |

Other ones:

| Option | Type | Default value | Usage | |-----------|---------|------------------------------|-----------------------------------------------------------------------------------| | --host | string | HOST if set or '0.0.0.0' | Specify server host | | --port | integer | PORT if set or 3001 | Specify server port | | --https | boolean | HTTPS if set or false | Specify if displayed url should use https |

Usage

Automatic initialisation

You really should use the sma-init command to configure your server, but see below to understand how tu use ServeMyApp

I only want to serve my frontend code in production

  • Build your application files (depends on your application framework/type):

     npm run build
  • Serve them.

     serve-my-app --src=build

Explanation:

  • --src: We're assuming that previous command will output compiled files to build folder.

I only want to add some api routes.

  • Create an api file as described below.

  • Serve it for development purpose:

    serve-my-app --srv=srv --watch
  • Add your routes

  • Serve it for production:

    serve-my-app --srv=srv

Explanation:

  • --srv: We're assuming that your api entrypoint is in srv folder.
  • --watch: Server will restart when a route will be added.

I want to serve both my frontend code and add some api routes.

Then :

  • For development purpose:

    • Launch the server

        serve-my-app --srv=srv --proxify --watch
    • Launch your frontend application (depends on your framework):

      npm run start/serve

    Explanation:

    • --srv: We're assuming that your api entrypoint is in srv folder.
    • --watch: Server will restart when a route will be added.
    • --proxify: All the relative xhr/fetch calls made in the application will be proxyfied to the server.
  • To run the server only once for production:

    • Build your app files (depends on your framework):

      npm run build
    • Launch the server

      serve-my-app --srv=srv --src=build

    Explanation:

    • --srv: We're assuming that your api entrypoint is in srv folder.
    • --src: We're assuming that previous command will output compiled files to build folder.

👍 In all cases, you can use relative urls for xhr/fetch calls in your code. 🚀 Again, all the ServeMyApp related commands and files can be generated using the sma-init cli tool.

FAQ

What should the server entrypoint look like?

It's a file exporting a function, which will receive the Express application as its first parameter, and the node http server as its second. With both you can do anything you want with your server.

For example:

const { json } = require('express');
const socketIO = require("socket.io");

module.export = (app, http) => {
    app.use(json());

    app.get('/foo', (req, res) => {
        res.json({msg: 'foo'});
    });

    app.post('/bar', (req, res) => {
        res.json(req.body);
    });
    
    socketIO(http).on("connection", client => {
      client.emit("message", "Welcome");
     });
}

I want to use some transpilation process for my server (Webpack, TypeScript...).

And you're right, it's a good idea!

In that case, you just have to handle the compilation process on your own, and specify an other folder for —srv files.

For example, for a TypeScript transpilation, and an "outDir": "../dist" in srv/tsconfig.json, you can update your script in package.json as:

"build:server": "rm -rf dist && tsc -p srv",
"sma": "serve-my-app --srv=dist [...]"

🤘 For Typescript transpilation ts-node will be used if your api entrypoint is detected as a typescript file. It allows you to use directly your typescript file for development:

"sma:dev": "serve-my-app --srv=srv --watch [...]"

😉 To help you in your Typescript development, you can import the ApiFunction type from 'serve-my-app':

import { ApiFunction } from 'serve-my-app'

const api: ApiFunction = (app, server) => {
 //
}

export default api

🔗 See a full example at: mathieutu/starter-kit-react-express

My package.json behaves in a weird manner: a proxy key comes and goes in it.

To proxify xhr calls from the front dev server to the express server, create-react-app uses a proxy key in the package.json. This is why this package adds it automatically if --proxify argument is provided.

This should be transparent for you, but if you want to disable it and handle yourself (or not) the proxy, you can do it by not passing the -—proxify flag.

You can find the documentation about proxies in create-react-app here.

Another question?

Please feel free to ask it!

License

This package is an open-sourced software licensed under the MIT license.

Contributing

Issues and PRs are obviously welcomed and encouraged, for new features as well as documentation.