Full Trust European Hosting

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

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.

 



Node.js Hosting Europe - HostForLIFE.eu :: How to Use Inheritance in Node.js ?

clock September 12, 2018 08:29 by author Peter

In this post we'll take a glance on inheritance of objects in Node.js. we'll learn to use utility module to achieve inheritance in Node.js. but confine mind that you simply will write plain JavaScript to attain inheritance in Node also.

You'll use Object.create() to inherit one object to a different in Node.js also.

In this post we'll learn to use util module to achieve inheritance in Node.js. First, you should to import util module in your application.
var util= require(‘util’);

After importing util module, allow us to say you have got an object as below,
function Student()

 this.name = "G Block";
 this.age = 40;
};


Just for demonstration allow us to add function in object using prototype,
Student.prototype.Display= function(){
 console.log(this.name + " is " + this.age + " years old");
 };


Next we tend to we progressing to make ArtsStudent object which is able to inherit Student object.
function ArtsStudent()
{
 ArtsStudent.super_.call(this);
 this.subject = "music";
 }; 
util.inherits(ArtsStudent,Student);


Second line of code in ArtsStudent object is very important,
ArtStudent.super_.call(this);

If you don’t call constructor of parent object as shown in above code snippet then on making an attempt to access properties of parent object will come undefined. In last line ArtStudent inherits Student using util.inherits() function ,

util.iherits(ArtsStudent,Student);

Next you can create instance of ArtsStudent and call function of parent object as below,
var a = new ArtsStudent();
a.Display();


Inheritance will be chained to any order. If you want you can inherit object from ArtsStudent as well. Inherited object will contain properties from both ArtsStudent and Student objects. So let us consider one more example,
function ScienceStudent()
{
 ScienceStudent.super_.call(this);
 this.lab = "Physics";
}
util.inherits(ScienceStudent,ArtsStudent);
var b = new ScienceStudent();
b.Display();

On this example ScienceStudent object inherits both Student and ArtsStudent objects. With this example, you can work with inheritance in Node.js using util module. I hope it works for you!

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 in Europe - HostForLIFE.eu :: Forgot Password Email Notification Using Node js

clock August 8, 2018 09:22 by author Peter

Before starting the code you need an account in send grid which will send the notification to your mail. Generally we use email notifications for forget passwords in your applications. You can follow the below steps to get the task done and if you have any queries please leave a comment below.

We require a few modules from npm to send the notification through the mail.

  • npm install formidable
  • npm install crypto
  • npm install async
  • npm install nodemailer

In my router.js file the following code will be present 
app.route('/forgotpasswordResponse') 
.post(userCtrl.forgotpasswordResponse); 


When I run my services and hit the above Url  from postman it will take you to the forgotpasswordResponse method. We are using post method in postman where we need to pass Email id as parameter

In forgotpasswordResponse my code is somthing like this,
exports.forgotpasswordResponse = function(req, res, next) { 

var input=req.body; 
//console.log(input); 
async.waterfall([ 
function(done) { 
    crypto.randomBytes(20, function(err, buf) { 
        var token = buf.toString('hex'); 
        done(err, token); 
    }); 
}, 
function(token, done) { 
    MongoClient.connect(url, function(err, db){  
        var dbo = db.db("Here is your DB Name"); 
        //console.log(req.body.Email); 
        var query = { Email : req.body.Email }; 
        dbo.collection('CLC_User').find(query).toArray(function(err,result){ 
            if(result.length == 0){ 
                req.flash('error', 'No account with that email address exists.'); 
            } 
            var myquery = { Email: result[0].Email }; 
            var newvalues = { $set: {resetPasswordToken: token, resetPasswordExpires: Date.now() + 3600000 }}; 
            dbo.collection("CLC_User").updateOne(myquery, newvalues, function(err, res) { 
                if (err) throw err; 
                console.log("1 document updated"); 
            }); 
             

           // console.log(result[0].Email); 
            done(err, token, result); 
        }); 
    }); 
}, 
function(token, result, done,Username,password) { 
    var emailVal = result[0].Email; 
    console.log(emailVal); 
    var Username=""; 
    var password=""; 
    MongoClient.connect(url, function(err, db){  
    var dbo = db.db("Here willbe your db name"); 
    dbo.collection('Accountsettings').find().toArray(function(err,result){ 
        if (err) throw err; 
        Username=result[0].UserName; 
        password=result[0].Password; 
       // console.log(Username); 
       // console.log(password); 
           // res.json({status : 'success', message : 'Records found', result : result}); 
     

    // console.log(Username); 
    var smtpTransport = nodemailer.createTransport({ 
        service: 'SendGrid', 
        auth: { 
          user: Username, 
          pass: password 
        } 
      }); 

    const mailOptions = { 
        to: emailVal, 
        from: '[email protected]', 
        subject: 'Node.js Password Reset', 
        text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' + 
            'Please click on the following link, or paste this into your browser to complete the process:\n\n' + 
            'http://' + req.headers.host + '/reset/' + token + '\n\n' + 
            'If you did not request this, please ignore this email and your password will remain unchanged.\n' 
    }; 
    smtpTransport.sendMail(mailOptions, function(err) {                
        console.log("HI:"+emailVal); 
        res.json({status : 'success', message : 'An e-mail has been sent to ' + emailVal + ' with further instructions.'});             
        done(err, 'done'); 
    }); 
}) 
}); 

 
], function(err) { 
if (err) return next(err); 
 
}); 


In my case  I am using waterfall methologie for this method with will execute acyn in method, In the above code initially I am updating the collection with resetPasswordToken and resetPasswordExpires using email id and getting my send  grid credentials from db form  Accountsettings collections. If you can observe in mailOptions text "req.headers.host" will be the link which will get in you mail with token.

When you click on Url which you got in the email it will redirect you to another page to set the password.

Again we need to go to route.js and the code will be some thing like this. It will take to html page which we can reset the password,
app.route('/reset/:token') 
.get(Resetpassword.resetpasswordResponse);  

This time I am passing the token which I stored in db as "resetPasswordToken". Now it will take you to resetpasswordResponse method and the code is below,
exports.resetpasswordResponse = function(req, res) { 
console.log("welcome"); 
MongoClient.connect(url, function(err, db){ 
var dbo = db.db("Here is you db"); 
dbo.collection('CLC_User').findOne({resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) { 
    if (!user) { 
        res.json({message: 'Password reset token is invalid or has expired.'}); 
    }else{ 
        console.log("coming"); 
        fs.readFile("api/Controllers/resetpassword.html", function (error, data) { 
            console.log("its working"); 
            if (error) { 
                console.log(error); 
                res.writeHead(404); 
                res.write('Contents you are looking are Not Found'); 
            } else { 
                //res.writeHead(200, { 'Content-Type': 'text/html' }); 
                res.write(data); 
            } 
            res.end(); 
        }); 
    } 
}); 
}); 


Your html code in resetpassword.html will be like this,
<!DOCTYPE html> 
<html> 
<head> 
<title>Reset Password</title> 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
</head> 
<body> 
<h4 class="postdata" style="text-align:center;"></h4> 
<div class="main-agileits"> 
<h2 class="sub-head">Reset Password</h2> 
<div class="sub-main">     
    <form method="post"> 
        <span class="senddata"></span><br><br> 
             
        <input placeholder="Enter Password" name="password" class="password" type="password" required=""><br><br> 

        <input placeholder="Confirm Password" name="confirmpassword" class="confirmpassword" type="password" required=""><br><br> 
         
        <input type="submit" name ="submit" value="RESET PASSWORD"> 
         
    </form> 
</div> 
</div> 
</body> 
</html> 


<script type="text/javascript"> 

$( document ).ready(function() { 
$("input[name='submit']").on("click", function(){ 
$(".senddata").html(""); 
var url = window.location.href; 
var password = $('.password').val(); 
var confirmpassword = $('.confirmpassword').val(); 

if( password == confirmpassword){ 
    $.post(url,{Password : password},function(result,status){ 
    var msg = result.status; 
    var msgdata = result.message; 
    if(msg == "success"){ 
        $(".postdata").html(msgdata); 
        $(".main-agileits").css("display","none") 
    }else{ 
        return false; 
    } 
}); 
}else{ 
    $(".senddata").html("Passwords did not match"); 
}        
return false; 
}); 

}); 

</script>


the next step is send the Email notification after changing the password.The code is
app.route('/reset/:token') 
.post(setpassword.setpasswordResponsemail); 

exports.setpasswordResponsemail = function(req, res) { 
async.waterfall([ 
function(done) { 
    MongoClient.connect(url, function(err, db){ 
        var dbo = db.db("Your Db name goes here");  
        dbo.collection('CLC_User').findOne({resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) { 
            if (!user) { 
                res.json({message: 'Password reset token is invalid or has expired.'}); 
            } 
            //console.log(user); 
            var myquery = { resetPasswordToken: req.params.token }; 
            var newvalues = { $set: {Password: req.body.Password,resetPasswordToken: undefined, resetPasswordExpires: undefined, modifiedDate : Date(Date.now()) }}; 
            dbo.collection("CLC_User").updateOne(myquery, newvalues, function(err, result) { 
                if (err) throw err; 
                //console.log("result ======" + result); 
                console.log("1 document updated"); 
            }); 
            done(err, user); 
        }); 
    }); 
}, 
function(user, done) { 
    MongoClient.connect(url, function(err, db){  
        var dbo = db.db("Your db name goes here"); 
        var Username=""; 
        var password=""; 
        dbo.collection('Accountsettings').find().toArray(function(err,result){ 
            if (err) throw err; 
            Username=result[0].UserName; 
            password=result[0].Password; 
        }) 
    }) 
    var smtpTransport = nodemailer.createTransport({ 
        service: 'SendGrid', 
        auth: { 
            user: Username, 
            pass: password 
        } 
    }); 
    var mailOptions = { 
        to: user.Email, 
        from: '[email protected]', 
        subject: 'Your password has been changed', 
        text: 'Hello,\n\n' + 
            'This is a confirmation that the password for your account ' + user.Email + ' has just been changed.\n' 
    }; 
    smtpTransport.sendMail(mailOptions, function(err) { 
        res.json({status : 'success', message : 'Success! Your password has been changed.'}); 
        done(err); 
    }); 

], function(err) { 
if (err) return err; 
}); 
}

Hope this code will help someone who really needs  it. Thank you.



Node.js Hosting in Europe - HostForLIFE.eu :: Node.js NPM Proxy Configurations

clock March 2, 2017 08:17 by author Peter

It is often required to work with Node.JS and NPM package installer behind the proxy. When trying to install npm packages behind the proxy, you may come across the error, given below.

To resolve exceptions, shown above, http_proxy and https_proxy configurations needs to be done for NPM. The step by step guidance is given below to perform the configuration on Windows machine-Go to C:\User\{User} directory.

  • if .npmrc file exists then open it in notpad editor else create .npmrcfile.
  • Add the lines, given below to the file for your valid AD domain user. proxy=http://domain\\username:password@ip:port
  • Add the lines, given below for Non AD user proxy=http://username:password@ip:port
  • Save .npmrc file
  • Try to use npm install now. It should work without any error.

The sample contents of .npmrc file are shown below.



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