Language: EN

que-es-y-como-usar-servor

Create Local Servers in Node.js with Servor

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

With Servor, we can quickly set up a development server without complications. It is designed to make the configuration and deployment of local servers easier.

Some of the features of Servor include:

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

For more information and documentation, we can visit the official Servor repository on GitHub. Here we will find additional details and usage examples that can help us make the most of this tool.

How to use Servor

Using Servor is quite straightforward. Here’s how we can set up 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 set up, we can run Servor by simply executing

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> <fallback> <port>
  • root Path to serve static files (defaults to the current directory .).

  • fallback The file served for all requests that do not match a file (defaults to index.html).

  • port The port from which files will be served (defaults to 8080).

Additionally, we can add the following parameters

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

How to use Servor from NodeJS.

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

npm install servor

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 execute 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,
});

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.