que-es-y-como-usar-servor

Create Local Servers in Node.js with Servor

  • 3 min

Servor is a Node.js tool designed to simplify the creation of local servers for web development.

With Servor, we can quickly spin up a development server without complications. It is designed to facilitate the setup and deployment of local servers.

Some of Servor’s features,

  • Fast development server: Spins up a server with a single command line.
  • Live reload: Automatically refreshes the browser when it detects changes in files.
  • Minimal configuration: No need for complex configuration files.

How to Use Servor

Using Servor is quite straightforward. Let’s see how we can start a local server and take advantage of its features.

npm install -g servor

How to Run Servor

Installing Servor is a simple and quick process. First, we need to have Node.js and npm installed on our system. Once we have this configured, we can run Servor simply by doing

npx servor

This global installation allows us to use the servor command from anywhere in our terminal.

The complete syntax allows for more options

npx servor

  • root Path to serve static files (default, the current directory .).

  • fallback The file served for all requests that don’t match a file (default, index.html).

  • port The port from which files will be served (default, 8080).

Additionally, we can add the following parameters

OptionDescription
--browseMakes the browser open when the server starts.
--reloadMakes the browser reload when files change.
--secureStarts the server with HTTPS using generated credentials.
--silentPrevents the Node server process from logging to stdout.
--moduleMakes the server wrap the root in module type tags in the script.
--staticMakes the server route nested index files if they exist.
--editorOpens a code editor (currently only vscode) at the project root.

How to Use Servor from NodeJS.

We can also run Servor directly from NodeJS. First, we need to add the package via

npm install servor
Copied!

Now we can add Servor as an action to our package.json like this,

{ “devDependencies”: { “servor”: “4.0.0” }, “scripts”: { “start”: “servor www index.html 8080 —reload —browse” } }

We can also run it from our JavaScript code. For example, to start a basic server with Servor, we simply run the following command in our project directory:

const servor = require('servor');
const instance = await servor({
  root: '.',
  fallback: 'index.html',
  module: false,
  static: false,
  reload: false,
  inject: '',
  credentials: null,
  port: 8080,
});
Copied!

This command will start a local server on port 8080 by default, serving static files from the current directory. We can access our application by opening http://localhost:8080 in our browser.