Node Js Code Samples



Filters


delete a folder in javascript
harika | 6 years, 2 months
// Delete the files recursively.
//fs is a built in node module.
const fs = require('fs');
fs.rmdir deletes the directory only if it empty. So for that we have to delete the files recursively
javascript node js
Command line arguments parsing in NodeJS with yargs and add a simple commands
kishore_kumar | 5 years, 7 months
const yargs = require("yargs")

yargs.command({
javascript node js
Check whether the file node is a directory or a plain file object
harika | 6 years, 2 months
//fs is a built in node module
const fs = require('fs');
const filePath = "/home/Desktop"
javascript node js
Write string or json objects directly to a file using node js
harika | 6 years, 2 months
const fs = require('fs');
//fs is a built in module in node js
const filePath = '/Desktop/Test.jso
javascript node js
Make a Get Request to url or to an api end point in Node JS
kishore_kumar | 5 years, 7 months
const request = require("request")

const url = "https://reqres.in/api/users"
javascript node js
Create a simple web server with express using Node js
kishore_kumar | 5 years, 7 months
const express = require('express')

const app = express()
express javascript node js
Create a model and save document in Mongo db using Mongoose and Node JS
kishore_kumar | 5 years, 7 months
const mongoose = require("mongoose")

mongoose.connect("mongodb://127.0.0.1:27017/my-db", {
mongoose javascript node js
Connect to Mongo DB and insert documents in Node JS
kishore_kumar | 5 years, 7 months
const mongodb = require("mongodb")
const mongo_client = mongodb.MongoClient

javascript node js mongodb
Run shell files in javascript
harika | 6 years, 5 months
//JS Code
function runShellScript(){
    const process = require('child_process'); 
keep the function code inside the js file wherever you want to use. and keep the shell file provided
javascript shell node js
Compress files synchronously in javascript
harika | 6 years
const tar = require('tar');
  tar.c(
    {
try to install 'tar' using npm and then try. Happy Coding :-)
node js javascript react js
Next