Node.js Core APIs Cheatsheet
Essential Node.js APIs: file system, HTTP, events, streams, and utilities.
| API | Description | Syntax | Example | Category |
|---|---|---|---|---|
| fs.readFile() | Read file contents asynchronously | fs.readFile(path, [encoding], callback) | fs.readFile("file.txt", "utf8", (err, data) => {}) | File System |
| fs.writeFile() | Write data to file | fs.writeFile(path, data, [encoding], callback) | fs.writeFile("file.txt", "content", "utf8", callback) | File System |
| fs.readFileSync() | Read file synchronously (blocking) | fs.readFileSync(path, [encoding]) | const data = fs.readFileSync("file.txt", "utf8") | File System |
| fs.appendFile() | Append data to file | fs.appendFile(path, data, callback) | fs.appendFile("log.txt", "new line\n", callback) | File System |
| fs.unlink() | Delete file | fs.unlink(path, callback) | fs.unlink("file.txt", (err) => {}) | File System |
| fs.mkdir() | Create directory | fs.mkdir(path, [mode], callback) | fs.mkdir("newdir", { recursive: true }, callback) | File System |
| fs.readdir() | Read directory contents | fs.readdir(path, callback) | fs.readdir(".", (err, files) => console.log(files)) | File System |
| fs.stat() | Get file/directory info | fs.stat(path, callback) | fs.stat("file.txt", (err, stats) => {}) | File System |
| fs.createReadStream() | Create readable stream | fs.createReadStream(path, [options]) | const stream = fs.createReadStream("large.txt") | Streams |
| fs.createWriteStream() | Create writable stream | fs.createWriteStream(path, [options]) | const stream = fs.createWriteStream("output.txt") | Streams |
| path.join() | Join path segments | path.join(...paths) | path.join("/home", "user", "file.txt") | Utilities |
| path.resolve() | Resolve absolute path | path.resolve(...paths) | path.resolve("./src", "index.js") | Utilities |
| path.basename() | Get filename from path | path.basename(path, [ext]) | path.basename("/path/to/file.txt") | Utilities |
| path.dirname() | Get directory from path | path.dirname(path) | path.dirname("/path/to/file.txt") | Utilities |
| path.extname() | Get file extension | path.extname(path) | path.extname("file.txt") | Utilities |
| http.createServer() | Create HTTP server | http.createServer((req, res) => {}) | const server = http.createServer((req, res) => res.end()) | HTTP |
| server.listen() | Listen on port | server.listen(port, [host], callback) | server.listen(3000, () => console.log("Running")) | HTTP |
| EventEmitter | Event emitter base class | const emitter = new EventEmitter() | emitter.on("event", () => {}) | Events |
| emitter.on() | Listen to event | emitter.on(eventName, callback) | emitter.on("data", (data) => console.log(data)) | Events |
| emitter.emit() | Emit event | emitter.emit(eventName, [data]) | emitter.emit("data", { value: 42 }) | Events |
| emitter.once() | Listen to event once | emitter.once(eventName, callback) | emitter.once("connection", () => {}) | Events |
| util.promisify() | Convert callback to promise | util.promisify(fn) | const readFile = util.promisify(fs.readFile) | Utilities |