app.get('/users', (req, res) => { res.json([{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }]); });
const user = new User({ name: 'John', age: 30 }); user.save((err) => { if (err) { console.error(err); } else { console.log('User saved successfully'); } });
Node.js can be used with various databases, including MongoDB, PostgreSQL, and MySQL. Mongoose is a popular ORM for MongoDB.
// Using async/await const fs = require('fs').promises; async function readFile() { try { const data = await fs.readFile('file.txt'); console.log(data.toString()); } catch (err) { console.error(err); } }
Node.js provides various testing frameworks, including Mocha and Jest.
Node.js provides various libraries for authentication and authorization, including Passport.js.
Node.js is built around asynchronous programming using callbacks, promises, and async/await. Understanding how to work with asynchronous code is crucial for building efficient and scalable applications.
const User = mongoose.model('User', userSchema);
mkdir myproject cd myproject npm init