res.end('Hello World\n');
});
server.listen(3000, () ⇒ { console.log('Server running at http://localhost:3000/');
});
```
== Code Example 2: Reading a File Asynchronously ==
```javascript
const fs = require('fs');
fs.readFile('/path/to/file', (err, data) ⇒ {if (err) throw err; console.log(data);}); ``` == Code Example 3: Writing to a File == ```javascript const fs = require('fs'); fs.writeFile('/path/to/file', 'Hello Node.js', (err) ⇒ {
if (err) throw err;
console.log('The file has been saved!');
});
```
== Code Example 4: Creating a Web Server ==
```javascript
const http = require('http');
http.createServer((request, response) ⇒ { response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
```
== Code Example 5: Using Modules ==
```javascript
const url = require('url');
const myUrl = url.parse('http://www.example.com');
console.log(myUrl.host);
```
== Code Example 6: Event Emitter ==
```javascript
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () ⇒ { console.log('an event occurred!');
});
myEmitter.emit('event');
```
== Code Example 7: Streaming Data ==
```javascript
const fs = require('fs');
const readStream = fs.createReadStream('/path/to/file');
readStream.on('data', (chunk) ⇒ { console.log(`Received ${chunk.length} bytes of data.`);
});
```
== Code Example 8: Using Promises ==
```javascript
const fs = require('fs').promises;
async function readFile(filePath) { try {
const data = await fs.readFile(filePath);
console.log(data.toString());
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
readFile('/path/to/file');
```
== Popular Third-Party Libraries ==
1. ** Express**: A fast, unopinionated, minimalist web framework for Node.js.
2. **Async**: Provides powerful utilities for working with asynchronous JavaScript.
3. ** Lodash**: A modern JavaScript utility library delivering modularity, performance, & extras.
4. ** Mongoose**: MongoDB object modeling tool designed to work in an asynchronous environment.
5. ** Socket.io**: Enables real-time, bidirectional, and
event-based communication.== Competition or Alternatives == Node.js competes with other server-side technologies and runtime environments: 1. ** Deno**: A secure runtime for JavaScript and TypeScript. 2. **Python (Django, Flask)**: Popular for web development and scripting. 3. ** Ruby on Rails**: A server-side web application framework written in Ruby. 4. **PHP**: Widely used for server-side web development. 5. **Java (Spring Framework)**: Offers robust support for building web applications. This overview provides a glimpse into Node.js, highlighting its capabilities, how to get started with development, and its place in the modern web development ecosystem. Node.js offers a versatile platform for building fast, scalable network applications, supported by a vibrant community and a rich ecosystem of modules and tools. ==Installation== ===Install on Windows with Chocolatey=== Chocolatey install via choco install https://community.chocolatey.org/packages/nodejs.install
- Update all first: choco upgrade all -y
==Node.js Bibliography==
