NexusAI GPT-4 Turbo
S
Can you build me a REST API with Node.js and Express? I need endpoints for users — CRUD operations with authentication.
I'll create a complete REST API with Express, JWT authentication, and user CRUD operations. Here's the setup:
server.js
const express = require('express'); const jwt = require('jsonwebtoken'); const bcrypt = require('bcryptjs'); const app = express(); app.use(express.json()); // Auth middleware const auth = (req, res, next) => { const token = req.header('Authorization'); if (!token) return res.status(401).json({ error: 'Access denied' }); try { req.user = jwt.verify(token, process.env.JWT_SECRET); next(); } catch(e) { res.status(400).json({ error: 'Invalid token' }); } }; app.listen(3000, () => console.log('Server running on :3000') );
This includes:
• JWT-based authentication middleware
• Password hashing with bcrypt
• RESTful route structure

Want me to add the CRUD route handlers and database connection next?
S
Yes! Add the CRUD routes and connect it to MongoDB with Mongoose.