Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

Node.js Hosting Europe - HostForLIFE.eu :: How to Create a Node.js Logging System with Express?

clock April 25, 2024 13:50 by author Peter

In contemporary web development, logging plays a crucial role in program monitoring and troubleshooting. Node.js offers a variety of logging tools for monitoring and analyzing application operations because of its event-driven architecture. This article will walk through how to build a logging system for a Node.js application using Winston, a versatile logging package, and Express, a popular web framework.

What is Logging?
The process of keeping track of activities and occurrences inside a software system is called logging. These log entries help with debugging, monitoring, auditing, and security. They include timestamps, severity levels, messages, and contextual data. Logging helps with performance analysis, intrusion detection, compliance adherence, and troubleshooting by capturing important information about system behavior. With their ability to provide insights into application flow, performance bottlenecks, user actions, and security concerns, logs are an indispensable tool for developers, administrators, and security professionals. Logging is essentially a basic procedure used in software development and operations that makes software systems easier to manage, secure, and monitor.

Why we use Logging?

Logging serves several essential purposes in software development:

  • Debugging and Troubleshooting: Logs are an important source of information for troubleshooting and identifying difficulties with a program. To comprehend the execution flow, spot faults, and find the source of problems, developers can go through log messages.
  • Monitoring and Performance Optimization: Developers may keep an eye on an application's performance in real time with the help of logs. Developers can pinpoint performance bottlenecks, maximize resource use, and raise the application's general effectiveness by examining log data.
  • Auditing and Compliance: Keeping track of user actions and system events is facilitated by logging. For compliance with legal standards like GDPR, HIPAA, or PCI DSS, this audit trail is crucial. Logs are useful for monitoring user behavior, spotting illegal access, and guaranteeing the accuracy of data.
  • Security and Intrusion Detection: When it comes to identifying security risks and unauthorized access attempts, logs are essential. Events including unsuccessful login attempts, questionable activity, and possible security breaches are recorded in security logs. Administrators are able to quickly identify and address security incidents by examining security logs.

Steps for Creating a Project
Step 1.  Setting Up the Project
Use my previous article for setting up Node.js, "How to upload file in Node.js" In this article, we mentioned important commands for uploading files in Node.js.

Step 2. Integrating Winston for Logging

Winston is a versatile Node.js logging package that supports many transports, such as file, database, and terminal. Winston will be set up to log messages to both the console and a log file at various levels (info, error, etc.).
// main.js

const winston = require('winston');

// Create a logger instance
const logger = winston.createLogger({
  level: 'info', // Logging level
  format: winston.format.json(), // Log format
  transports: [
    // Log to the console
    new winston.transports.Console(),
    // Log to a file
    new winston.transports.File({ filename: 'logging_file.log' })
  ]
});


Step 3.  Implementing Logging Middleware in Express
We are able to intercept and handle incoming requests thanks to express middleware. A unique middleware function will be developed to log incoming HTTP requests. Request methods, URLs, and other pertinent data will be recorded by this middleware and logged using Winston.
// Middleware to log requests
app.use((req, res, next) => {
  logger.log('info', `${req.method} ${req.url}`);
  next();
});

Step 4.  Creating Routes and Handling Errors
After installing logging middleware, we will create our Express application's routes. To show how to handle requests, we'll construct a simple route for the main page ("/"). Furthermore, error handling middleware will be implemented to log and manage any faults that arise during request processing.
// Route handling
app.get('/', (req, res) => {
  res.send('Hello, world!');
});

// Error handling middleware
app.use((err, req, res, next) => {
  logger.log('error', err.stack);
  res.status(500).send('Something broke!');
});


Step 5. Serving Log File via Web Interface
We'll develop an additional route ("/logs") that reads the contents of the log file and sends it to the client in order to give a user-friendly way for log files to be viewed. In order to ensure security and avoid dangerous infinite loops, this route will ignore logging requests so they are not kept in the log file.
// Route to serve log file
app.get('/logs', (req, res) => {
  fs.readFile('combined.log', 'utf8', (err, data) => {
    if (err) {
      logger.log('error', err);
      return res.status(500).send('Error reading log file');
    }
    res.type('text/plain').send(data);
  });
});


Step 6. Running the Application

Finally, let us launch our Express application and verify its operation. To start the HTTP request logging, start the server and visit the home page. You may also view the contents of the log file using the web interface by going to the "/logs" route.
node main.js

Output



Node.js Hosting Europe - HostForLIFE.eu :: TODO application with CQRS Design Pattern within Nest JS

clock December 4, 2023 08:53 by author Peter

A software design pattern called CQRS (Command Query Responsibility Segregation) divides up who is responsible for accessing and writing data in a system. The same set of components are frequently used in traditional design patterns for both reading and writing data. CQRS introduces two distinct models to suggest the division of these duties.

 

  • Command Model: The command model handles event triggering, data store updating, and command processing and validation.
  • Query Model: This model manages the processes involved in getting data out of the system.

The four major concepts of CQRS are listed below:

  • Command: Action or request to modify the system's status
  • Query: An occurrence or a request to obtain data from the system
  • Command Handler: An element in charge of processing commands and adjusting the system's state.
  • Query Handler: An element in charge of managing queries and getting information out of the system.

CQRS Offers Advantages like:

  • Scalability: Because the application supports several read and write data models, it can be scaled.
  • Flexibility: It permits the use of distinct data stores that are best suited for writing and reading processes.
  • Performance: There is room for improvement with this application.

This CQRS Model may be used with Nest JS; in this tutorial, we'll discover how to do it.

We'll use the creation and retrieval of a TODO application as an example using the CQRS paradigm.

Application for TODOs using CQRS pattern
You must use the command line to install nest cli if it is not already installed globally on your computer.
npm i -g @nestjs/cli

Create new Nest Js project if not created already using below command
nest new todo-application

Dependency Installation
Nest JS provides a package to implement CQRS, we need to install the package first using below command
npm install --save @nestjs/cqrs

Module Creation
Create new Module for our TODO application using command
nest generate module todo

Commands
Commands are used to change the application state, when a command is triggered, it is handled by corresponding command handler. Then handler will be responsible to process the operation. Every command will have command handler in order to process the command

Create new file inside todo module with name create-todo-command.ts and implement the below code
import { ICommand } from '@nestjs/cqrs';

export class CreateToDoCommand implements ICommand {
    constructor(
      public readonly title: string,
      public readonly description: string,
    ) {}
}

Command Handler
Create new file inside todo module with name create-todo-command-handler.ts  and implement the below code

import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { CreateToDoCommand } from './create-todo-command';

@CommandHandler(CreateToDoCommand)
export class CreateToDoHandler implements ICommandHandler<CreateToDoCommand> {
  async execute(command: CreateToDoCommand): Promise<void> {
    // Add Logic to do validation and business rule
    const { title, description } = command;

    // Use Repository to save directly or Create Factory to add business logic and save
  }
}


Query
Queries are used to retrieve the data from the application, when a query is requested, Query handler handles the requests and retrieves the data. Every query will have query handler

Create new file inside todo module with name get-todo-query.ts  and implement the below code
import {  IQuery } from '@nestjs/cqrs';

export class GetToDoQuery implements IQuery {
    constructor() {}
}

Query Handler
Create new file inside todo module with name get-todo-query-handler.ts  and implement the below code
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { GetToDoQuery } from './get-todo-query';

@QueryHandler(GetToDoQuery)
export class GetToDoQueryHandler implements IQueryHandler<GetToDoQuery> {
  async execute(query: GetToDoQuery): Promise<any> {
    // Fetch data using repository or factory and return it
    // Sample Response
    return [
      { id: 1, title: 'Test', description: 'Reminder to complete daily activity' },
      { id: 2, title: 'Test 2', description: 'Reminder to complete daily activity2' },
    ];
  }
}


Module
In the todo.module.ts  file, import the CQRS module to use the command handlers and query handlers
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { ToDoController } from './todo.controller';
import { CreateToDoHandler } from './create-todo-command-handler';
import { GetToDoQueryHandler } from './get-todo-query-handler';

@Module({
  imports: [CqrsModule],
  controllers: [ToDoController],
  providers: [CreateToDoHandler, GetToDoQueryHandler],
})
export class ToDoModule {}


Controller
Create todo.controller.ts  file to handle the API request and use command and query bus
import { Controller, Get, Post, Body } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { CreateToDoCommand } from './create-todo-command';
import { GetToDoQuery } from './get-todo-query';

@Controller('ToDo')
export class ToDoController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
  ) {}

  @Post()
  async createToDo(@Body() body: { title: string, description: string }): Promise<void> {
    const { title, description } = body;
    await this.commandBus.execute(new CreateToDoCommand(title, description));
  }

  @Get()
  async getToDo(): Promise<any[]> {
    return this.queryBus.execute(new GetToDoQuery());
  }
}

Pro Tips
You can create a separate folder for command and query to segregate the code and make it more readable. Also, here I just gave the basic high level demo, but in your application, you can create dto for command and query, and you can directly use that dto
Conclusion

In conclusion, while CQRS can provide advantages in terms of scalability and flexibility, it comes with increased complexity and potential challenges in terms of consistency and development overhead. It is important to carefully consider whether the benefits align with the specific requirements and goals of the application being developed.

HostForLIFE.eu Node.js Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.




Node.js Hosting Europe - HostForLIFE.eu :: How to Choose PDF Library in Node.js?

clock October 13, 2023 09:54 by author Peter

When it comes to document sharing, Adobe's Portable Document Format (PDF) is critical for maintaining the integrity of text-rich and aesthetically pleasing data. Access to online PDF files often necessitates the use of a certain application. Many prominent digital publications now need PDF files. Many companies utilize PDF files to create expert documentation and invoices. Additionally, developers usually employ PDF document generating libraries to meet specific client requirements. The introduction of contemporary libraries has simplified the process of creating PDFs.

What exactly is Node.js?
Node.js is a cross-platform, open-source server environment that works with Windows, Linux, Unix, macOS, and other operating systems. Node.js is a JavaScript back-end runtime environment that uses the V8 JavaScript engine to execute JavaScript code outside of a web browser.

Developers can utilize JavaScript to construct server-side scripts and command-line tools with Node.js. Dynamic web page content is commonly built before a page is sent to a user's web browser by utilizing the server's ability to run JavaScript code. Node.js promotes a "JavaScript everywhere" paradigm that unifies online application development around a single programming language, as opposed to using several languages for server-side and client-side programming.

const PDFDocument = require('pdfkit');
const fs = require('fs');
const doc = new PDFDocument();
doc.text('Hello world', 100, 100)
doc.end();
doc.pipe(fs.createWriteStream('Output.pdf'));


PDFmake

A wrapper library for PDFKit is called pdfmake . The programming paradigm is where there is the most difference:

While pdfmake uses a declarative approach, pdfkit uses the traditional imperative technique. Because of this, concentrating on what you want to perform is simpler than spending time instructing the library on how to get a particular outcome.

But not everything that glitters is gold, and using Webpack and trying to integrate bespoke fonts may cause problems. Unfortunately, there isn't much information regarding this problem available online. If you don't use Webpack, you can still easily clone the git repository and run the script for embedded font.

var fonts = {
  Roboto: {
    normal: 'fonts/Roboto-Regular.ttf',
    bold: 'fonts/Roboto-Medium.ttf',
    italics: 'fonts/Roboto-Italic.ttf',
    bolditalics: 'fonts/Roboto-MediumItalic.ttf'
  }
};

var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
var fs = require('fs');

var docDefinition = {
  // ...
};

var options = {
  // ...
}

var pdfDoc = printer.createPdfKitDocument(docDefinition, options);
pdfDoc.pipe(fs.createWriteStream('output.pdf'));
pdfDoc.end();


jsPDF
Among the PDF libraries on GitHub, jsPDF is a PDF generation library for browsers. It has the most starts, and this is not a coincidence given how reliable and well-maintained it is. Because the modules are exported in accordance with the AMD module standard, using them with nodes and browsers is simple.

For PDFKit, the offered APIs follow an imperative paradigm, making it difficult to create complicated layouts. Including typefaces The only additional step is to convert the fonts to TTF files, which is not difficult. Although jsPDF is not the simplest library to use, the extensive documentation ensures that you won't run into any specific difficulties when using it.

import { jsPDF } from "jspdf";
const doc = new jsPDF();
doc.text("Hello world!", 10, 10);
doc.save("output.pdf");

Puppeteer
Puppeteer is a Node library that offers a high-level API to manage Chrome, as you may know, but it can also be used to as PDF generator. Because the templates must be written in HTML, jsPDF is fairly simple for web developers to use.

Puppeteer has mostly two drawbacks. You must put a backend solution in place. Puppeteer must be launched each time a PDF needs to be created, adding to the burden. It moves slowly.

It might be an excellent solution if the aforementioned drawbacks are not a major issue for you, especially if you need to construct HTML tables and other such things.
const puppeteer = require('puppeteer')

async function printPDF() {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto('www.google.com', {waitUntil: 'networkidle0'});
  const pdf = await page.pdf({ format: 'A4' });
  await browser.close();
  return pdf
})


While pdfmake is based on PDFKit, pdf-lib is a library for producing and editing PDFs that is entirely written in Typescript. Even though it was launched after all the other libraries, it has thousands of stars on GitHub, indicating how well-liked it is.

The APIs have a fantastic design and naturally function with both browsers and nodes.It offers many features that other libraries simply don't have, including PDF merging, splitting, and embedding;

Although it is quite powerful, pdf-lib is also very user-friendly. One of the most popular features is the ability to embed font files using Unit8Array and ArrayBuffer, which enables using fs while dealing with nodes and xhr when working in the browser.

When you compare it to other libraries, you'll be able to tell that it performs better, and you can utilize Webpack with it, of course. Additionally, this library uses an imperative approach, which makes it difficult to work with complex layouts.
import { PDFDocument } from 'pdf-lib'

// PDF Create
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage()
page.drawText('Hello World')
const pdfBytes = await pdfDoc.save()

IronPDF
IronPDF for Node.js renders PDFs from HTML strings, files, and web URLs by using the robust Chrome Engine. It is advised to assign this operation to the server side since rendering can be computationally demanding. In order to offload the computational effort and await the outcome, frontend frameworks like ReactJs and Angular can communicate with the server. The outcome can then be shown on the front end side.

Software engineers may produce, modify PDF documents, and extract PDF material with the use of the IronPDF library, which was created and maintained by Iron Software.

When it comes to
    the creation of PDF documents using HTML, URL, JavaScript, CSS, and a variety of image formats
    A signature and headers should be included.
    Add, Copy, Split, Merge, and Delete PDF Pages
    Can able to include CSS properties.
    Performance improvement Async and complete multithreading support

import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
  const pdf = await PdfDocument.fromHtml("<h1>Hello World</h1>");
  await pdf.saveAs("Output.pdf");
})();




Node.js Hosting Europe - HostForLIFE.eu :: npm vs yarn vs pnpm

clock September 20, 2023 08:39 by author Peter

Let's look at the differences between npm, yarn, and pnpm in this blog. Package managers such as npm, yarn, and pnpm are extensively used in the JavaScript ecosystem to manage dependencies and packages for Node.js projects. They take different approaches and have distinct features, which can affect how they manage packages and interact with your project.

npm

npm is the default package manager for Node.js and is included with the installation of Node.js. It has a lengthy history and is frequently used in the JavaScript ecosystem. To store and distribute packages, npm makes use of a centralized package registry known as the npm registry. It adds a "node_modules" directory to your project and installs all project dependencies there.

// Install a package
npm install package-name

//Update a package
npm update package-name

// Remove a package
npm uninstall package-name


yarn

// Install a package
yarn add package-name

// update a package
yarn upgrade package-name

// Remove a package
yarn remove package-name


pnpm

pnpm is another package manager for Node.js projects. It aims to solve the issue of disk space usage by using a unique approach. Instead of creating a separate "node_modules" directory for each project, pnpm uses a single global package store and creates symlinks to the required packages in each project's "node_modules" directory. This can significantly reduce the amount of disk space used by your projects.

// Install a package
pnpm add package-name

// update a package
pnpm update package-name

//Remove a package
pnpm remove package-name


HostForLIFE.eu Node.js Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.




Node.js Hosting Europe - HostForLIFE.eu :: Using Immer to Navigate State Management in JavaScript

clock September 6, 2023 09:48 by author Peter

Wrangling application state is a vital skill in the dynamic arena of web development. Keeping code tidy and predictable can be difficult, but the introduction of tools like Immer has provided a breath of fresh air in this sector. Immer has won the hearts of developers all over the world with its ease of use and efficiency in dealing with state changes. This essay will take you on a journey into the world of Immer, looking into its practical implementation, benefits, and how it makes the sometimes difficult chore of state management in JavaScript a lot easier.

Immer, lovingly created by Michel Weststrate, is a library that changes the way we deal with data immutability. It enables us to write code that appears to modify state directly while actually orchestrating the construction of new, immutable state structures.1. Starting Out: InstallationLet's begin your Immer trip by installing the library with npm or yarn.
npm install immer

2. Basic Usage
At the core of Immer is a function called produce. It's the magic wand that takes your current state and a function containing your desired changes and works its enchantment.

import produce from 'immer';

const initialState = { count: 0 };

const nextState = produce(initialState, draftState => {
  draftState.count += 1;
});


3. Taming Nesting
Now comes the pièce de résistance. Immer's prowess shines when dealing with deeply nested objects and arrays. It simplifies complex state structures with ease.
const complexState = {
  user: {
    name: 'Alice',
    address: {
      city: 'Wonderland'
    }
  }
};

const newState = produce(complexState, draftState => {
  draftState.user.name = 'Bob';
  draftState.user.address.city = 'Dreamland';
});


Benefits
    Crystal Clarity: Immer brings lucidity to your code. Instead of fretting about cloning and immutability, you can focus on the changes you wish to make.
    Performance Star: The magic of Immer not only simplifies your code but optimizes performance by reducing memory overhead and avoiding redundant copying.
    Readable Harmony: Code created with Immer mirrors traditional mutable code, fostering understanding and maintainability.

Conclusion

Immer has reimagined the landscape of state management in JavaScript applications. Its knack for handling immutable updates effortlessly, coupled with an approachable syntax, is nothing short of revolutionary. By streamlining the intricate dance of cloning and immutability checks, Immer reduces error risks and transforms the development experience.

In the ever-evolving world of web development, where efficiency and maintainability are paramount, Immer emerges as a steadfast ally. Whether you're working on a modest project or an intricate application, incorporating Immer into your state management arsenal offers cleaner, bug-free code and boosts developer productivity.

So, as you embark on your coding escapades, remember that Immer isn't just a library; it's a compass that guides you through the labyrinth of state management, making your journey more delightful and your code more enchanting.



Node.js Hosting Europe - HostForLIFE.eu :: How Do I Use Multer to Upload Multiple Files in Node.js?

clock July 21, 2023 08:46 by author Peter

This post will teach you how to upload numerous files in Node.js. In Node.js, we upload files using many libraries. In this article, we discuss a really basic and simple method for beginners, complete with examples. There is also a "rar" file attached to export your system and execute it properly.


Steps for Starting a Project
Step 1: Create a Node.js Project

Use my previous article "How to Upload File in Node.js" for Node.js setup. In this article, we discussed crucial Node.js commands for uploading files.

Step 2: Project Organization.
When step one is finished, it produces some directories such as node modules, package-lock.json, and package.json, but we still need to build some files listed below the image. When the project is executed, the 'upload' folder is automatically created, and all uploaded files are saved to the 'upload' folder.

In this file structure, node_modules, package-lock.json, and package.json these files is created while you set up a project. We created index.js and views folder. Below attached all files used in this project.

Step 3. Create an index.js file.
In this file, have the "/" route and other necessary code required to run the project below the file is attached.

index.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const { render } = require('ejs');
var fs = require("fs");
const multer = require('multer');
const app = express();

// Middleware setup
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// Set up static file serving for 'upload' folder
const uploadsPath = path.join(__dirname, 'upload');
app.use('/upload', express.static(uploadsPath));

// Route to render the 'first' view
/**
 * @route GET /
 * @description Render the 'first' view.
 */
app.get("/", function (req, res) {
    res.render('first');
});

// Set up multer storage and upload configuration
const storage1 = multer.diskStorage({
    destination: function (req, file, cb) {
        // Check the fieldname and store files accordingly
        if (file.fieldname === 'file1' || file.fieldname === 'file2' || file.fieldname === 'file3') {
            cb(null, path.join(__dirname, '/upload'));
        } else {
            cb(new Error('Invalid field name'));
        }
    },
    filename: (req, file, cb) => {
        cb(null, file.originalname); // Use the original file name
    }
});
const uploadd = multer({ storage: storage1 });

// Configure multer fields for file uploads
/**
 * @typedef {Object} MulterField
 * @property {string} name - The name of the field for file upload.
 * @property {number} maxCount - The maximum number of files allowed to be uploaded for this field.
 */

/**
 * @type {MulterField[]} fields - Multer fields configuration for file uploads.
 */
const cpUpload = uploadd.fields([
    { name: 'file1', maxCount: 1 },
    { name: 'file2', maxCount: 8 },
    { name: 'file3', maxCount: 8 }
]);

// Route to handle file upload
/**
 * @route POST /fileupload
 * @description Handle file upload.
 * @returns {void}
 * @throws {Error} If an invalid field name is provided.
 */
app.post('/fileupload', cpUpload, (req, res) => {
    res.redirect('/');
});

// Start the server
/**
 * @description Start the server and listen on port 3001.
 * @event
 */
app.listen(3001, () => {
    console.log('server is running on port http://localhost:3001');
});

A server that can handle file uploads is created with the Node.js Express code that is provided. Multer, a well-known middleware library, is used to control file uploads. To render views, EJS is used as the templating engine. For the fields with the names "file1," "file2," and "file3," the application accepts file uploads. The 'upload' folder on the server is where the uploaded files are kept. Other developers will find it simpler to comprehend the functions of various routes and middlewares thanks to the inclusion of JSDoc comments in the code documentation. The 'first' view is rendered by the server at the root route, which operates on port 3001. When a file is successfully uploaded, the server returns users to the home page.

Step 4. Create first.ejs file.
This file has simple HTML and CSS code.

first.ejs
<!DOCTYPE html>
<html>
<head>
  <title>File Upload</title>
  <style>
    /* CSS styles for the file upload form */

    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 20px;
    }

    .container {
      max-width: 400px;
      margin: 0 auto;
    }

    .form-group {
      margin-bottom: 20px;
    }

    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }

    input[type="file"] {
      padding: 5px;
      border: 1px solid #ccc;
      border-radius: 4px;
      width: 100%;
    }

    input[type="submit"] {
      background-color: #4CAF50;
      color: white;
      padding: 10px 15px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    input[type="submit"]:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>File Upload</h2>
    <!-- File upload form -->
    <form action="/fileupload" method="post" enctype="multipart/form-data">
      <!-- File 1 input -->
      <div class="form-group">
        <label for="file1">File 1:</label>
        <input type="file" id="file1" name="file1">
      </div>
      <!-- File 2 input -->
      <div class="form-group">
        <label for="file2">File 2:</label>
        <input type="file" id="file2" name="file2">
      </div>
      <!-- File 3 input -->
      <div class="form-group">
        <label for="file3">File 3:</label>
        <input type="file" id="file3" name="file3">
      </div>
      <!-- Upload button -->
      <input type="submit" value="Upload">
    </form>
  </div>
</body>
</html>


Explanation

Three file input fields are created in a file upload form using the specified HTML code. Users can upload files by choosing them and submitting the form. The form gains visual components thanks to the CSS styles. The chosen files are sent to the "/fileupload" endpoint using the "post" method and "multipart/form-data" encoding when the form is submitted. The uploaded files are processed by the server, which manages the endpoint. Developers can better understand and manage the functioning of the form by using the HTML comments, which offer succinct explanations of the code's function and organization.

Output

 

 



Node.js Hosting Europe - HostForLIFE.eu :: How To Use File Handling In Node.js?

clock July 14, 2023 07:53 by author Peter

This article covers fundamental file operations in node.js, including Reading, Writing, Updating, and Deleting files. Whether it involves accessing configuration files, processing user-uploaded files, or persisting data to disc, file management is an integral part of many applications. Node.js's "fs" (file system) module is indispensable for streamlining file-handling procedures. It provides a vast array of functions and methods for completing various file-related tasks.

Node.js operation using file management

  • Creating File Reading File
  • Modifying File
  • Delete File

How do you create a Node.js file?

Node.js offers a rapid and efficient method for creating files, which is a necessary task for many applications. In this post, we will examine a variety of synchronous and asynchronous Node.js file generation methods. We will discuss how to use the fs (file system) module, how to handle errors, and best practices for effective file creation. At the conclusion of this article, you will have a comprehensive comprehension of how to create and write files in Node.js, enabling you to handle file-related tasks competently.

Code

const fs = require('fs');

// Specify the file path and content
const filePath = 'path/to/newFile.txt';
const fileContent = 'This is the content of the new file.';

// Asynchronous file creation and writing
fs.writeFile(filePath, fileContent, 'utf8', (err) => {
  if (err) {
    console.error('Error creating  file:', err);
    return;
  }

  console.log('File is  created successfully.');
});

// Synchronous file creation and writing
try {
  fs.writeFileSync(filePath, fileContent, 'utf8');
  console.log('File created successfully.');
} catch (err) {
  console.error('Error creating  file:', err);
}

Explanation
Using the writeFile method, a file can be created asynchronously by providing the file path, the content, the encoding, and a callback function to manage success or failure. A try-catch block is used to manage errors when using the writeFileSync method to create and write a file synchronously.
How is a file read in Node.js?

Reading a file in Node.js involves obtaining access to and retrieving the file's contents from the file system. It necessitates reading file data using the asynchronous or synchronous readFile or readFileSync methods of the fs module. Asynchronous reading permits non-blocking I/O operations while synchronous reading prevents code execution. Error management is essential for addressing issues such as file not found and permissions errors. Applications that require file-based data processing, such as reading configuration files, parsing user uploads, or extracting data from databases, must have the capability to read files.

Code
const fs = require('fs');

// Specify the file path
const filePath = 'path/to/file.txt';

// Asynchronous file reading
fs.readFile(filePath, 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading the file:', err);
    return;
  }

  console.log('File contents:', data);
});

// Synchronous file reading
try {
  const data = fs.readFileSync(filePath, 'utf8');
  console.log('File contents:', data);
} catch (err) {
  console.error('Error reading the file:', err);
}

Explanation
The code shows how to read files in Node.js. ReadFile is used for asynchronous reading, and a callback function is used for error handling. In a try-catch block, readFileSync is used for synchronous reading. If no errors happen, both techniques log file contents. While synchronous reading halts code execution until the file is read, asynchronous reading permits parallel tasks.

How to update a file in Node.js?
In the context of Node.js, updating a file is the act of editing or adding data to an already-existing file on the file system. It entails opening the file with the fs (file system) module, making the required adjustments, and then writing the revised content back into the file. A file can be updated by performing operations like replacing certain lines or entries, appending new data at the end, or replacing specific blocks of text. The usual procedure for this is to read the data in the file, update or add to it as necessary, and then write the updated data back into the file. Developers can dynamically update the contents of files, maintain data integrity, and reflect the most recent information in applications that rely on file-based data storage by updating files in Node.js.

Code
const fs = require('fs');

// Specify the file path
const filePath = 'path/to/file.txt';

// Specify the updated content
const updatedContent = 'This is the updated content of the file.';

// Asynchronous file updating
fs.writeFile(filePath, updatedContent, 'utf8', (err) => {
  if (err) {
    console.error('Error updating the file:', err);
    return;
  }

  console.log('File updated successfully.');
});

// Synchronous file updating
try {
  fs.writeFileSync(filePath, updatedContent, 'utf8');
  console.log('File updated successfully.');
} catch (err) {
  console.error('Error updating the file:', err);
}

Explanation
The file location, the modified content, and optional encoding are passed to the writeFile method, which is used to update the file asynchronously. The identical operation is carried out synchronously using the writeFileSync function. Try-catch blocks are used for error handling, and success or error messages are logged appropriately.

How to delete a file in Node.js?

The fs (file system) module is used in Node.js to delete files by deleting them from the file system. Applications frequently use it to perform resource cleanup, temporary file deletion, and file storage management. The fs.unlink method is offered by Node.js for asynchronous file deletion. This method runs a command using the file path as an argument function when the deletion is finished or if there is a problem. To handle situations when the file cannot be erased because of permissions or if the file doesn't exist, error handling is essential. Node.js has fs.unlinkSync for synchronous file deletion, but due to its blocking nature, it should only be used in extreme cases. Developers may effectively manage files, ensure that unwanted or superfluous files are removed from the file system, and maintain a tidy and organized file storage environment by utilizing Node.js's file deletion capabilities.

Code
const fs = require('fs');

// Specify the file path
const filePath = 'path/to/file.txt';

// Asynchronous file deletion
fs.unlink(filePath, (err) => {
  if (err) {
    console.error('Error deleting the file:', err);
    return;
  }

  console.log('File deleted successfully.');
});

// Synchronous file deletion
try {
  fs.unlinkSync(filePath);
  console.log('File deleted successfully.');
} catch (err) {
  console.error('Error deleting the file:', err);
}


Explanation
First, require("fs") is used to import the fs module. The file path and a callback function are passed to the unlink method as its first and second arguments, respectively, before the file is asynchronously deleted. Whenever something is finished or if there is an error, the callback function is called. The callback console is used for handling errors. By means of console.log(), success and errors are recorded.

The synchronous variant of unlink is the unlinkSync function. It utilizes identical inputs and deletes the file synchronously. A try-catch block is used to handle errors, and console.error() is used to log errors. Console.log() is used to record a successful deletion. The specified file will be eliminated from the file system after executing this code. Ensure that the location of the file you want to remove is substituted for "path/to/file.txt".
Conclusion

File handling in Node.js is an essential component of development since it makes it possible to manipulate files effectively. A complete collection of functions to read, write, update, and delete files are offered by the fs module. Developers can ensure non-blocking I/O and enhance application performance by utilizing asynchronous operations. For reliable file management, proper error handling is essential. Reliability and security are improved by adhering to best practices such as validating file paths, controlling permissions, and performance optimization. Developers may do a range of file-related operations with Node.js, from managing user uploads to working with configurations. Developers may create sophisticated applications with seamless file manipulation capabilities thanks to Node.js' effective file management.  

FAQs

Q. Can I read a file in Node.js synchronously?
A. Yes, you can read a file synchronously in Node.js using the fs.readFileSync method. However, it's generally recommended to use asynchronous file reading (fs.readFile) to avoid blocking the execution of other code.

Q. How can I handle errors while performing file operations in Node.js?
A. Error handling is essential in file operations. You can handle errors using try-catch blocks for synchronous operations or by providing a callback function that captures errors for asynchronous operations. Additionally, you can listen for error events emitted by file streams.

Q. How can I delete a file in Node.js?
A. You can delete a file in Node.js using the fs.unlink method for asynchronous deletion or fs.unlinkSync for synchronous deletion. Both methods require the file path as a parameter.

Q. What precautions should I take while handling files in Node.js?
A. When handling files, it's important to validate file paths to avoid security vulnerabilities. Ensure that you have proper file permissions to perform the desired operations. Additionally, consider using asynchronous operations to avoid blocking the event loop and optimize performance.

Q. Can I update a specific portion of a file in Node.js?
A. Yes, you can update specific portions of a file in Node.js. However, you would need to read the file, make the necessary modifications to the data, and then write the updated data back to the file.



Node.js Hosting - HostForLIFE.eu :: 10 Reasons Why "Node.js" Is A First Choice For Web-App Development

clock December 4, 2020 09:00 by author Peter

Node.js was created by Rayn Dahl in 2009 and his work was supported by Joyent. The core idea behind its development was extending Javascript into something that can not only run in the browser but also operate on the machine as a standalone application.
 
What can Node.JS do? Can you use it to build your first highly-secured application?
 
If you are asking these questions, then you are in the right place. Today, we are going to inform you why there’s so much hype among the developers when it comes to Node.js.
 
With so many technologies for development, it can be tough to choose the one which you can easily master yet it can give you better results. Besides, as a beginner, it’s way tougher to choose. So why should you go for Node.js? What makes it so special? Let’s get started from the basics.
 
Node.js was created by Rayn Dahl in 2009 and his work was supported by Joyent. The core idea behind its development was extending Javascript into something that can not only run in the browser but also operates on the machine as a standalone application.
 
Along with Javascript, Node.js runs on the specific Javascript runtime engine, i.e., V8. This runtime engine takes your code from Javascript and transforms it into rapid machine code.
 
Besides, several top-notch apps like Uber, PayPal, Netflix, etc. state that Node.js has powered their web applications and has provided a much faster interface.
 
Why Node.js?
 
Node.js is a Javascript runtime environment that promotes open-source and cross-platform functionalities. It helps in the execution of Javascript outside a browser. With the help of Node.js, one can create a dynamic web application or web page by writing and running a command-line for server-side scripting before the page is being shared at the user’s end.
 
It provides a unique blend of helpers, libraries, and other tools that make the web app development process efficient, easier, and simpler to operate. Besides, it offers a powerful base to develop web apps while securing an online presence.
 
Node.js uses a non-blocking, event-driven I/O Model that turns it light and efficient. It has one of the largest open-source libraries ecosystems, NPM. Besides, it uses push technology on web sockets that allows 2-way communication between server and client. One of the perfect examples of this feature of Node.js is Chatbots. You might have come across one of those while visiting a website’s customer service as well.
 
So now that you have a clear understanding of what you can do with Node.js, let’s get to the details that make it astounding!
 
Reasons that Make Node.js Exceptional!
 
Fast & Scalable
The scalability that Node.js provides to an organization has boosted their profits. As we have already discussed that Node.js runs on V8, its speed in terms of computing is unbeatable. With the new JS code conversion into the native language, the outcoming speed of operation has inspired several large and small institutions.
Besides, Node.js can help you with its ability to run a large batch of asynchronous processes simultaneously. Unlike other technologies for development, Node.js can complete reading, writing, or modifying a database in a shorter timeline.
 
Supremely Extensible
Another vital feature of Node.js is its extensibility. According to the requirements you have, the capabilities it has can be constructed and extended. For any developer who wants to share data among the web server and client, Node.js is there for your aid. It saves the coder from modulating differences in syntax while writing for the backend.
 
Easy To Learn & Code
From the very beginning, Javascript has been introduced in the coding world. It has improved and evolved itself with the internet. That means, almost every programmer or developer has a little bit of Javascript knowledge. But for those who don’t know what the heck is Javascript, it’s the basic and simple language that anyone can efficiently learn in minimum time.
 
As the V8 engine is created for JS coding and deployment by Google Chrome, it makes your work problem-free, and easy. So to get fabulous deployment results, all you need to do is code with JS along with Node.js and your stunning web-app is on its way!
 
Enhanced Productivity
Being entirely based on Javascript, Node.js removes the requirement for having different developers. Be it front-end or back-end, you can easily do it with Node.js instead of relying on other programming languages to complete the task which in return increases productivity.
 
Pervasive Runtime
With the arrival of Node.js, Javascript has been freed from the limitation of the environment as well. Now you can use JS on the client-side along with the server-side.
 
Regardless of where you are manipulating with the files, the effects can easily be seen on the other side.
 
Data Streaming
When it comes to Data Streaming, Node.js can effectively handle both input and output requests to support the online streaming functionality. It uses data streams to run certain operations at the same time it processes data.
 
Single Codebase
As you can write code in JS on both server and client-side, Node.js makes code execution and deployment faster and easier. Moreover, as language conversion is not required in Node.js, the data can be easily transferred from client to server and vice-versa.
 
NPM
NPM or Node.js Package Module enables different environmental packages to indulge into the existing one. It makes the development and performance robust, consistent, and quicker. There are more than 6000 modules available in Node.js that competes with ruby and will soon surpass it.
 
Database Query Resolutions
With Node.js working for both front-end and back-end, there is no need for you to worry about the translation of codes which also promotes flawless streaming while easily solving the database queries by itself.
 
Proxy Server
Node.js acts like a proxy server that gathers data resources and gives the third-party app enough time to perform the requested/required actions.
 
Conclusion
Node.js comes with plenty of benefits which makes it an adequate choice for developing a web application. While using it in your next project, you can not only assure less turnaround time, but also ensure an amazing output level.
 
If you want to empower yourself as a developer and you want the user of your web application to utilize the application to its highest extent in order to yield desirable outcomes, then Node.js is an ideal alternative.
 
Overall, it would not be wrong to say that Node.js has become the first choice for web app developers. There are several reasons Node.js has flourished so much and will undoubtedly reach great heights in the application development industry. It gives you what you want so you can offer creative solutions.



Node.js Hosting - HostForLIFE.eu :: Uploading File in Node.js

clock November 18, 2020 07:38 by author Peter

In this article we will observe uploading a file on a web server made use Node.js. Stream in Node.js makes this task super simple to upload files or so far as that is concerned working with any information exchange between a server and a client. To transfer a file we will work with two modules, HTTP and fs. So let us begin with stacking these two modules in an application:

var http = require('http');
var fs = require('fs')


When modules are loaded proceed to and make a web server as below:
http.createServer(function(request,response){   
  }).listen(8080);


So far we are great and now we wish to utilize the accompanying procedure:
Make a destination write stream. In this stream the substance of the uploaded file will be written. We need to compose once again to the client the rate of data being uploaded.

The first requirement could be possible utilizing a pipe. A pipe is an event of stream in Node.js. And the request is a readable stream. So we will use a pipe event to write a request to a readable stream.
var destinationFile = fs.createWriteStream("destination.md");     
      request.pipe(destinationFile);


The second necessity is to give back an of data uploaded. To do that first read the aggregate size of the file being uploaded. That could be possible by reading the content-length (line number 1 in the accompanying code snippet). At that point in the data occasion of request we will update uploadedBytes that starts at zero (line number 2). In the data event of the request we are calculating the percentage and writing it back in the response.

Now, It’s time to putting it all together your app should contain the following code to upload a file and return the percentage uploaded.
var http = require('http');
var fs = require('fs');
  http.createServer(function(request,response){    
    response.writeHead(200);
      var destinationFile = fs.createWriteStream("destination.md");      
      request.pipe(destinationFile);
      var fileSize = request.headers['content-length'];
      var uploadedBytes = 0 ;
      request.on('data',function(d){  
          uploadedBytes += d.length;
          var p = (uploadedBytes/fileSize) * 100;
          response.write("Uploading " + parseInt(p)+ " %\n");
     });
      request.on('end',function(){
            response.end("File Upload Complete");
          });
    }).listen(8080,function(){        
        console.log("server started");
         });

On a command prompt start the server as in the picture below:

Presently let us utilize curl -upload-file to upload a file on the server.

As you see, while the file is being uploaded the percentage of data uploaded is returned once again to the client. So thusly you can upload a file to the server made utilizing Node.js. Hope this tutorial works for you!



Node.js Hosting Europe - HostForLIFE.eu :: Sending Email Using Node.JS

clock May 22, 2019 07:42 by author Peter

For sending an email using Node.js, we need a node package called nodemailer. Before this, we need to set up the Node environment. To download the latest version of Node, click this link and download.

Once downloaded, install the local environment and make it ready. For confirming the Node installation, open command prompt and type the following command and press enter.
node -v

We will get the currently installed version of Node.js. After completion of all this, follow the below steps.

Install the nodemailer package using Node.js command prompt with this command.
npm install nodemailer –s

Once installed, the package.json file will be modified with the dependencies of the nodemailer package.
After installing nodemailer package, import the file to our node.js file which we are using for sending an email. For importing any node package, we need to use -
var nodemailer = require('nodemailer'); 

Here, we have created an instance for nodemailer package as nodemailer. Now, we need to use the createTransport method for assigning the host and credentials for authentication. Find the below one.
var transporter = nodemailer.createTransport({ 
    host: 'mail.yourserver.com', 
    auth: { 
        user: [email protected]', 
        pass: 'password' 
      } 
})

Once we're finished with the above things, we need to construct the list of objects which are required to send an email. Check below.
var mailOptions = { 
    from: emailFrom, 
    to: emailTo, 
    cc: emailCc, 
    bcc: emailBcc, 
    subject: emailSubject, 
    html: emailContent 
  };


Now, we need to use the sendMail method with transporter instance as mentioned below. For this method, we need to pass the mailOptions variable which contains the details of sending an email and we have added the callback function.
transporter.sendMail(mailOptions, function(error, info) { 
        if (error) 
        { 
            res.send([{ 
                result: "failed" 
            }]); 
        } 
        else 
        { 
            res.send([{ 
                result: "success" 
            }]); 
        } 
    }); 


Now, the below part will summarize everything.
const express = require('express'); 
const app = express(); 
var nodemailer = require('nodemailer'); 
 
app.get('/sendmail', (req, res) => { 
  var transporter = nodemailer.createTransport({ 
    service: 'gmail', 
    auth: { 
      user: '[email protected]', 
      pass: 'gmail_account_password' 
    } 
  }); 
  var mailOptions = { 
    from: 'DAEMON <[email protected]>', 
    to: '[email protected]', 
    cc: '[email protected]', 
    bcc: '[email protected]', 
    subject: 'Reg: Send Email using node JS', 
    html: 'Welcome to Node JS' 
  }; 
transporter.sendMail(mailOptions, function (error, info) { 
    if (error) { 
      res.send([{ 
        result: "failed" 
      }]); 
      console.log("failed" + error); 
    } else { 
      res.send([{ 
        result: "success" 
      }]); 
    } 
  }); 
}); 
const port = process.env.PORT || 3000; 
app.listen(port); 
console.log('API server started on: ' + port); 

Just copy this all code and place it into a Node file and name that as Emailsending.js.
Now, open the Node js Command Prompt and execute the Node.js file.
For the execution of node js file, use
node file_name.js

 

Once the Node.js file is executed, just check this URL http://localhost:3000/sendmail. Here, sendMail is the get method for the above program.

HostForLIFE.eu Node.js Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

 



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in