Express JS Assignment- 1
Basic Questions
- Install Node.js and verify its version using the terminal.
- Create a simple index.js file and print “Hello Express!” in the console.
- Install express module using npm.
- Create your first basic Express.js server that listens on port 3000.
- Create a GET route at / that returns “Welcome to Express.js”.
- Create another route /about that returns “About Page”.
- Create a route /contact that returns “Contact Page”.
- Create a route /runtime-info that returns JSON with Node.js version and a string saying “Express loaded successfully”.
- Demonstrate the use of req (request) object by printing request headers in the console.
- Demonstrate the use of res (response) object by sending a JSON response.
- Define a route with a query parameter: /search?keyword=node. Log the keyword.
- Define a route with a route parameter: /user/:id. Return the user id.
- Show how to set up nodemon for auto server restart.
- Create a route /hello/:name which responds with “Hello {name}”.
- Organize your Express.js project into routes and controllers folder.
- Create a .env file and load environment variables using dotenv.
- Show how to use app.use(express.json()) to parse JSON body in POST requests.
- Create a POST route /login that accepts username and password in request body.
- Demonstrate handling of async function with async/await inside a route.
- Build one basic server without Express (pure Node.js http module) returning “Hello World”, and another with Express returning “Hello Express!”. Compare code length.
Intermediate Questions
- Create /framework-info route that returns a JSON object with keys { framework: “Express”, alternative: “Django” } so client sees the comparison in structured data.
- Create a route /products that returns an array of sample products in JSON.
- Create a POST route /add-user that accepts a user object (name, email).
- Demonstrate request validation by ensuring that name and email are provided in /add-user.
- Create a PUT route /update-user/:id that updates user data.
- Create a DELETE route /delete-user/:id that removes a user.
- Show how to set a custom HTTP header in a response.
- Demonstrate returning a 404 error response when a route is not found.
- Make two endpoints: /query-demo?fruit=apple (log & return fruit) and /param-demo/:color (return color). Demonstrate difference using code.
- Create a simple middleware that logs each request method and URL.
- Demonstrate using Promises inside a route to simulate fetching data.
- Demonstrate using async/await for the same fetch operation.
- Show how to split routes into a separate file (routes/users.js) and use them in index.js.
- Create a GET route /download that sends a text file to the client.
- Create a POST route /feedback and validate that feedback message is not empty.
- Write code to handle both JSON and URL-encoded form data in requests.
- Use environment variables to define the server port instead of hardcoding.
- Create a middleware that blocks requests if a query parameter token is missing.
- Create an API that simulates delay using setTimeout with async/await.
- Demonstrate how to handle promise rejection gracefully inside a route.
Advanced Questions
- Build a small Express.js app with routes: /, /users, /posts, each returning JSON data.
- Implement request validation on /register to check if email format is valid and password length ≥ 6.
- Write a middleware that measures the time taken for each request and logs it.
- Create a route /weather/:city that simulates fetching weather data asynchronously.
- Implement a global error handler middleware in Express.js.
- Create a route /profile which should only allow access if a ?auth=true query is present.
- Implement async/await in /posts route that returns posts after 2 seconds (simulate database).
- Create an Express app with two routes (/sync-error and /async-error) that both throw errors in different ways, and handle them properly using a global error‑handling middleware.
- Organize project structure into routes, controllers, middlewares, config with example code.
- Implement route /register that must validate: email format valid, password ≥ 6 length. If validation fails, send 400 response.