Quick Start

How to set up your Servify.js server in under 5 minutes

Setup

You need at least Node.js 6.0.0 installed along with NPM. If you have not already installed, you can download it here at the Node.js website.

Make sure your current project already has a package.json in its working directory. If it does not, use

npm init

and fill out the information. This will create a file that looks something like this:

{
  "name": "examples",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Installing

Your project is now set up for you to use npm and install "packages." These packages will be used in your JS project. The next thing to do is install Servify.js

npm install servify.js --save

This installs all of the required files to a new folder under the working directory called node_modules.

Working With Servify

You should already have a JavaScript file created in the working directory. If you do not then create one. You can name it whatever you want, but it is standard to call it main.js or index.js.

Basics

First of all, you want to require the Servify.js module and initialize the application. This is where everything runs through. Think of it as a wrapper for the server, router and other things.

const App = require("servify.js")
const servify = new App()

Now create a new server. The server, by default will run on 127.0.0.1 (localhost) on port 80 (HTTP port) or whatever port is specified as the environment variable.

servify.createServer()

Now the server is setup!

Running Your Server

To run the server run the file containing the code you used above. For example, if your main JS file's name was index.js then you would run this in your command line (ie. CMD, Powershell, Terminal, etc.)

node index.js

This will launch the server. Now go to 127.0.0.1/ in your browser. If you changed the port to something other than 80, then change accordingly.

This should open up a page that displays Hello, World!

Last updated