1. NodeJS
Node.js has revolutionized server-side development, empowering developers to build scalable, high-performance web applications with JavaScript – the language of the web. In this wiki, you’ll find a wealth of resources covering a wide range of topics, from the basics of asynchronous programming and event-driven architecture to advanced techniques like building RESTful APIs, microservices, and real-time applications.
1. What is Node.js?
Section titled “1. What is Node.js?”- A runtime environment to execute JavaScript outside the browser.
- Built on Chrome’s V8 JavaScript engine.
- Uses event-driven, non-blocking I/O.
Use Cases
Section titled “Use Cases”- Web servers
- RESTful APIs
- Real-time applications (e.g., chat)
3. 📁 Setting Up Node.js
Section titled “3. 📁 Setting Up Node.js”Installation
Section titled “Installation”- Install from nodejs.org
- Verify with:
Terminal window node -vnpm -v
Initialize a Project
Section titled “Initialize a Project”npm init3. 📦 Creating a Basic HTTP Server
Section titled “3. 📦 Creating a Basic HTTP Server”const http = require('http');
const server = http.createServer((req, res) => { res.write('Hello from Node.js server!'); res.end();});
server.listen(3000);4. ✅ Handle Basic Get and Post
Section titled “4. ✅ Handle Basic Get and Post”You can handle simple GET and POST requests using just Node.js and the http module — no Express needed.
4.1 Basic GET and POST Handling
Section titled “4.1 Basic GET and POST Handling”const http = require('http');
const server = http.createServer((req, res) => { const { method, url } = req;
// Set content type res.setHeader('Content-Type', 'application/json');
if (method === 'GET' && url === '/') { res.writeHead(200); res.end(JSON.stringify({ message: 'Hello from GET!' }));
} else if (method === 'POST' && url === '/data') { let body = '';
// Collect the POST data req.on('data', chunk => { body += chunk.toString(); });
// Handle end of data req.on('end', () => { const parsedData = JSON.parse(body); res.writeHead(200); res.end(JSON.stringify({ message: 'Received POST data!', data: parsedData })); });
} else { res.writeHead(404); res.end(JSON.stringify({ error: 'Not found' })); }});
server.listen(3000, () => { console.log('Server running on http://localhost:3000');});4.2 Testing It
Section titled “4.2 Testing It”-
GET request:
Visithttp://localhost:3000/in your browser → responds with JSON. -
POST request (e.g. using curl or Postman):
curl -X POST http://localhost:3000/data \ -H "Content-Type: application/json" \ -d '{"name":"Nadith", "age":17}'================================
Removing Node
To uninstall Node.js from aaPanel, follow one of the methods below depending on how you installed it:
✅ If You Installed Node via aaPanel App Store:
Section titled “✅ If You Installed Node via aaPanel App Store:”- Go to aaPanel > App Store
- Scroll or search for Node.js
- Click the “Bin icon” (Uninstall) next to Node.js
This will cleanly remove Node.js and any linked paths managed by aaPanel.
🧨 If Node.js Was Installed Manually (via source, NVM, or apt):
Section titled “🧨 If Node.js Was Installed Manually (via source, NVM, or apt):”You need to remove it manually depending on the method:
🅰️ APT-based Uninstall (Debian/Ubuntu)
Section titled “🅰️ APT-based Uninstall (Debian/Ubuntu)”sudo apt remove nodejssudo apt purge nodejssudo apt autoremove🅱️ NVM-based Uninstall
Section titled “🅱️ NVM-based Uninstall”If you used nvm:
nvm uninstall <version># Example:nvm uninstall 18Or to remove all:
rm -rf ~/.nvmAlso remove from .bashrc or .zshrc:
# Remove lines like theseexport NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"🆎 Remove Global Folders (Optional Clean-Up)
Section titled “🆎 Remove Global Folders (Optional Clean-Up)”sudo rm -rf /usr/local/bin/nodesudo rm -rf /usr/local/bin/npmsudo rm -rf /usr/local/lib/node_modules✅ Confirm Uninstall
Section titled “✅ Confirm Uninstall”Run:
node -vnpm -vIf they show “command not found”, Node.js is fully removed.