Express JS Assignment- 1

 Basic Questions

  1. Install Node.js and verify its version using the terminal.
  2. Create a simple index.js file and print “Hello Express!” in the console.
  3. Install express module using npm.
  4. Create your first basic Express.js server that listens on port 3000.
  5. Create a GET route at / that returns “Welcome to Express.js”.
  6. Create another route /about that returns “About Page”.
  7. Create a route /contact that returns “Contact Page”.
  8. Create a route /runtime-info that returns JSON with Node.js version and a string saying “Express loaded successfully”.
  9. Demonstrate the use of req (request) object by printing request headers in the console.
  10. Demonstrate the use of res (response) object by sending a JSON response.
  11. Define a route with a query parameter: /search?keyword=node. Log the keyword.
  12. Define a route with a route parameter: /user/:id. Return the user id.
  13. Show how to set up nodemon for auto server restart.
  14. Create a route /hello/:name which responds with “Hello {name}”.
  15. Organize your Express.js project into routes and controllers folder.
  16. Create a .env file and load environment variables using dotenv.
  17. Show how to use app.use(express.json()) to parse JSON body in POST requests.
  18. Create a POST route /login that accepts username and password in request body.
  19. Demonstrate handling of async function with async/await inside a route.
  20. 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

  1. Create /framework-info route that returns a JSON object with keys { framework: “Express”, alternative: “Django” } so client sees the comparison in structured data.
  2. Create a route /products that returns an array of sample products in JSON.
  3. Create a POST route /add-user that accepts a user object (name, email).
  4. Demonstrate request validation by ensuring that name and email are provided in /add-user.
  5. Create a PUT route /update-user/:id that updates user data.
  6. Create a DELETE route /delete-user/:id that removes a user.
  7. Show how to set a custom HTTP header in a response.
  8. Demonstrate returning a 404 error response when a route is not found.
  9. Make two endpoints: /query-demo?fruit=apple (log & return fruit) and /param-demo/:color (return color). Demonstrate difference using code.
  10. Create a simple middleware that logs each request method and URL.
  11. Demonstrate using Promises inside a route to simulate fetching data.
  12. Demonstrate using async/await for the same fetch operation.
  13. Show how to split routes into a separate file (routes/users.js) and use them in index.js.
  14. Create a GET route /download that sends a text file to the client.
  15. Create a POST route /feedback and validate that feedback message is not empty.
  16. Write code to handle both JSON and URL-encoded form data in requests.
  17. Use environment variables to define the server port instead of hardcoding.
  18. Create a middleware that blocks requests if a query parameter token is missing.
  19. Create an API that simulates delay using setTimeout with async/await.
  20. Demonstrate how to handle promise rejection gracefully inside a route.

Advanced Questions

  1. Build a small Express.js app with routes: /, /users, /posts, each returning JSON data.
  2. Implement request validation on /register to check if email format is valid and password length ≥ 6.
  3. Write a middleware that measures the time taken for each request and logs it.
  4. Create a route /weather/:city that simulates fetching weather data asynchronously.
  5. Implement a global error handler middleware in Express.js.
  6. Create a route /profile which should only allow access if a ?auth=true query is present.
  7. Implement async/await in /posts route that returns posts after 2 seconds (simulate database).
  8. 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.
  9. Organize project structure into routes, controllers, middlewares, config with example code.
  10. Implement route /register that must validate: email format valid, password ≥ 6 length. If validation fails, send 400 response.