Node.js and Express
Notes from the summary of COMP3011 Lecture 6, 7, and online resources.
Introduction to Node.js and Express.js
Node.js is a runtime environment that allows you to run JavaScript on the server side (outside the browser).
Express.js is a web framework built on top of Node.js to simplify server-side development.
JavaScript Libraries
Popular frontend libraries and frameworks include:
- React – for building interactive UIs.
- Vue.js – lightweight and beginner-friendly.
- jQuery – DOM manipulation and AJAX (older but still relevant).
Key Concepts
Node.js (Backend)
- JavaScript runtime outside the browser.
- Built on Chrome’s V8 engine.
- Asynchronous, event-driven, scalable.
- Used for backend services and APIs.
REST (Representational State Transfer)
- Web service architecture using standard HTTP methods.
- Stateless communication.
- CRUD operations:
- GET: Retrieve data.
- POST: Create new data.
- PUT: Update existing data.
- DELETE: Remove data.
Express.js
- Node.js web application framework.
- Simplifies web server and API development.
- Provides routing, middleware, and HTTP handling.
Practical Implementation
- Setup:
- Install Node.js and npm.
npm initto initialize a project.npm install expressto add Express.js.
- Basic Web Server:
- Use
app.get(),app.post(),app.put(),app.delete(). - Serve HTML via
res.sendFile(). - Use
nodemonfor auto-reload.
- Use
- Route Parameters & Query Strings:
- Extract and handle values from URLs.
- Form Handling:
- Use
body-parserto process form data.
- Use
- Testing:
- Use Postman to test API endpoints.
- Validation:
- Use Joi for input validation.
- Security:
- Intro to basic authentication concepts.
Key Takeaways
- Node.js + Express.js = powerful backend stack.
- RESTful API design is crucial.
- Tools like Postman and Joi streamline development.
- Middleware is fundamental for building robust Express apps.
Advanced Express, MongoDB, Templates
Revision
- Nodemon: Auto restart server.
- Joi: Request validation.
- Body-parser: Parses incoming request bodies.
Middleware in Express
- Middleware functions have access to
req,res,next. - Types:
- Application-level:
app.use() - Router-level:
router.use() - Error-handling:
(err, req, res, next) - Built-in:
express.static,express.json() - Third-party:
body-parser,cookie-parser,morgan
- Application-level:
Authentication & Authorization
- Authentication: Verifying identity.
- Authorization: Determining access rights.
- JWT: Stateless authentication via JSON Web Tokens.
npm install jsonwebtoken
- Passport.js:
- Popular middleware for user authentication.
- Supports local and OAuth strategies.
- Example:
passport-local,express-sessionsetup.
MongoDB
- NoSQL document database (stores JSON-like documents).
- Collections = Tables; Documents = Rows.
- Use MongoDB Atlas for cloud hosting.
Mongoose (ODM)
- Defines Schemas for MongoDB collections.
- Create and manage documents with Models.
npm install mongoose- Connect with
mongoose.connect(). - CRUD operations:
.find(),.findOne(),.deleteOne(),.save()
- Handle IDs with
mongoose.Types.ObjectId()
Templates
- Used for rendering HTML with dynamic content.
EJS
- Install:
npm install ejs - Setup:
app.set('view engine', 'ejs') - Files:
views/index.ejs
Pug
- Install:
npm install pug - Setup:
app.set('view engine', 'pug') - Syntax is whitespace-sensitive.
- Load static files via
express.static('public')
AJAX
- Use jQuery
ajax()for DELETE requests in the front-end.
Exercises
- Practice building an Express + MongoDB app.
- Render tasks with Pug.
- Add delete buttons with AJAX handling.
- Serve static files (CSS/JS) with
express.static().
Summary
- Covered middleware, authentication, MongoDB integration with Mongoose, and template rendering using EJS and Pug.
- Built full-stack capabilities with modern JavaScript tools.
Leave a comment