A simple yet comprehensive Express.js web server demonstrating various routing techniques, including basic routes, dynamic parameters, query strings, conditional routing, regular expressions, and error handling.
- ✅ Basic routing (
/,/about) - ✅ Conditional routing with
next()middleware (/foo) - ✅ Regular expression routes (
/user(name)?) - ✅ Dynamic route parameters (
/user/:username) - ✅ Query string handling (
/get) - ✅ 404 error handling
- Node.js (v14 or higher)
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/nhapham03/MyExpressServer.git
cd ExpressServer- Install dependencies:
npm installStart the server with:
npm startThe server will run on port 3000 by default. You can access it at http://localhost:3000
For development with auto-restart on file changes:
npm run dev- URL:
GET / - Response:
"Hello World" - Example:
http://localhost:3000/
- URL:
GET /about - Response:
"About page" - Example:
http://localhost:3000/about
- URL:
GET /foo - Response: Randomly returns either:
"sometimes this"(50% chance)"and sometimes that"(50% chance)
- Example:
http://localhost:3000/foo
- URL:
GET /userorGET /username - Response:
"User route matched" - Examples:
http://localhost:3000/userhttp://localhost:3000/username
- URL:
GET /user/:username - Response:
"Hello {username}" - Example:
http://localhost:3000/user/john→"Hello john"
- URL:
GET /get?param1=value1¶m2=value2 - Response:
"Check console for query parameters" - Note: Query parameters are logged to the console
- Example:
http://localhost:3000/get?name=John&age=25
- URL: Any undefined route
- Response:
"404 - Not Found" - Example:
http://localhost:3000/nonexistent
express-web-server/
├── server.js # Main application file
├── server.test.js # Test suite
├── package.json # Project dependencies
└── README.md # Documentation
Created as part of a Node.js/Express.js learning assignment.
ISC
[https://youtu.be/PMrY1dqfNkM]
The video demonstrates:
- Server startup
- All route functionalities