Node

Accelerate Web Development with Node.js on Linespace

Node.js is a powerful, JavaScript-based platform built on Google Chrome’s V8 Engine. It is widely used for developing high-performance, I/O-intensive web applications such as video streaming sites, single-page applications, and other real-time applications. Node.js is open source, free to use, and trusted by developers around the world.

Why Choose Node.js?

Node.js simplifies web application development with a rich library of JavaScript modules and offers a seamless runtime environment. It is particularly useful for:

  • I/O-bound applications
  • Data streaming applications
  • Data-intensive real-time applications (DIRT)
  • JSON API-based applications
  • Single-page applications

Setting Up Node.js on Linespace

To use Node.js on Linespace, you need to ensure your hosting plan includes Node.js instances. If your account shows 0 of 0 Node.js instances, you can order additional instances from the Add/Upgrade Services section in the control panel.

  1. Click “Create Instance” in your hosting control panel.
  2. Configure the following settings:
    • Node.js Script Path: Select the exact .js file you want to run.
    • Node.js Version: The latest version is recommended, but you can select other available versions.
    • Dedicated IP (Optional): If you need a fixed IP address to run your instance on a custom domain or subdomain, you can purchase a dedicated IP.
    • NODE_ENV (Optional): Used to define whether the environment is for development or production.
  3. Click “Add” to create your Node.js instance.

Connecting to MySQL in Node.js

To establish a MySQL connection, use socketPath instead of the database host. Below is a sample script:

const mysql = require('mysql');
const connection = mysql.createConnection({
  user: 'db_name',
  password: 'db_pass',
  database: 'db_name',
  socketPath: '/var/run/mysqld/mysqld.sock'
});
connection.connect((err) => {
  if (err) throw err;
  console.log('Connected!');
});

Node Package Manager (NPM)

NPM is an essential tool that provides:

  • Online repositories for Node.js modules
  • A command-line utility for installing, managing, and updating Node.js dependencies

To use NPM, SSH access must be enabled on your hosting plan. If it is not available, you can purchase it from the Add/Upgrade Services section.

Installing Modules with NPM

To install a Node.js module, use:

$ npm install <module-name>

For example, to install the popular express framework:

$ npm install express

Then, in your JavaScript file, use:

var express = require('express');

To check installed modules, run:

$ npm list

Installed modules will be stored in /home/node_modules/.

Node.js provides a fast and scalable solution for modern web applications. Whether you’re building real-time applications, APIs, or complex web platforms, Node.js on Linespace offers a reliable and efficient environment for your development needs.

Start leveraging the power of Node.js today!

Leave a Reply

Your email address will not be published. Required fields are marked *