// Import 'child_process' module
const { exec } = require('child_process');
// Execute 'ls -l' command
exec('ls -l', (error, stdout, stderr) => {
// Check for errors
if (error) {
console.error('Error:', error);
return;
}
// Log the result
console.log('Result:', stdout);
});Execute Shell Command using 'child_process' module
javascript
Continue Reading
Discover more amazing content handpicked just for you
Tutorial
Build a Custom Rate Limiter in Node.js with Redis
Use Postman or curl:
curl http://localhost:3000Apr 04, 2025
Tutorial
javascript css +1
Building a Real-Time Chat Application with WebSockets in Node.js
mkdir websocket_chat
cd websocket_chatnpm init -yAug 03, 2024
Code
php
JWT Token Creation and Verification in Node.js using 'jsonwebtoken'
// Import 'jsonwebtoken' module
const jwt = require('jsonwebtoken');
// Secret key for signing and verifying JWT tokens
const secret = 'mysecretkey';
// Create a JWT token with a payload and set expiration to 1 hour
const payload = { user_id: 123, username: 'john_doe' };
const token = jwt.sign(payload, secret, { expiresIn: '1h' });
// Verify a JWT token
try {
const decoded = jwt.verify(token, secret);
console.log(decoded);
} catch (error) {
console.error('Token verification failed.');
}Jan 26, 2024
Code
javascript
Simple HTTP Server in Node.js
No preview available for this content.
Jan 26, 2024
Code
javascript
Read and Write Files in Node.js using 'fs' module
No preview available for this content.
Jan 26, 2024
Code
javascript
Simple RESTful API in Node.js using Express
// Import 'express' module
const express = require('express');
const app = express();
// Define a route for accessing a resource
app.get('/api/resource', (req, res) => {
res.json({ message: 'Resource data' });
});
// Start the server and listen on port 3000
app.listen(3000, () => {
console.log('RESTful API is running on port 3000');
});Jan 26, 2024
Code
javascript
Date Manipulation and Sum Calculation
No preview available for this content.
Jan 26, 2024
Code
javascript
Access Command-line Arguments
// Access command-line arguments excluding the first two elements (node and script path)
const args = process.argv.slice(2);
// Log the command-line arguments
console.log('Command-line arguments:', args);Jan 26, 2024
Code
javascript
Set and Access Environment Variable
No preview available for this content.
Jan 26, 2024
Code
javascript
Event Emitter using 'events' module
No preview available for this content.
Jan 26, 2024
Code
javascript
Construct File Path using 'path' module
No preview available for this content.
Jan 26, 2024
Code
javascript
Basic Authentication using 'express-basic-auth' middleware
// Import required modules
const express = require('express');
const app = express();
const basicAuth = require('express-basic-auth');
// Define a user for basic authentication
const users = { 'username': 'password' };
// Use express-basic-auth middleware for authentication
app.use(basicAuth({ users }));
// Define a route accessible only to authenticated users
app.get('/secure', (req, res) => {
res.send('Authenticated route');
});Jan 26, 2024
Code
javascript
Hashing Password with SHA-256 using 'crypto' module
No preview available for this content.
Jan 26, 2024
Code
javascript
Parse URL and Query Parameters
No preview available for this content.
Jan 26, 2024
Code
javascript
File Stream Copy using 'fs' module
No preview available for this content.
Jan 26, 2024
Code
javascript
Simple WebSocket Server using 'ws' library
No preview available for this content.
Jan 26, 2024
Discussion 0
Please sign in to join the discussion.
No comments yet. Start the discussion!