TechBuzzPro

Build an MVC Web App from the Terminal: Step-by-Step

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can build a working MVC web app entirely from the terminal by installing a runtime, creating a project with a framework CLI, generating a model-view-controller structure, wiring routes, and running a local server. This guide uses Node.js with Express and a simple file-based MVC layout because it needs no database setup and works on Windows, macOS, and Linux. The same terminal-first workflow applies to Rails, Laravel, Django, or ASP.NET Core if you prefer another stack.

What You'll Need

  • A computer running Windows 10/11, macOS, or a mainstream Linux distribution.
  • Node.js LTS installed from the official nodejs.org download page (includes npm).
  • A terminal: Terminal on macOS/Linux, Windows Terminal or PowerShell on Windows.
  • A plain text editor or IDE such as VS Code, opened from the terminal with code . if installed.
  • Basic comfort typing commands and editing files. No prior MVC experience required.

Step-by-Step

  1. Verify your tools. In the terminal run node -v and npm -v. You should see version numbers. If not, install Node.js LTS from the official site and reopen the terminal.
  2. Create the project folder. Run mkdir mvc-app && cd mvc-app (on PowerShell use mkdir mvc-app; cd mvc-app). Then run npm init -y to create package.json.
  3. Install Express. Run npm install express. This adds Express to node_modules and records it as a dependency.
  4. Create the MVC folders. Run mkdir models views controllers public. On PowerShell, separate the names with commas or run the command once per folder. These four folders hold your data logic, templates, request handlers, and static assets.
  5. Add a start script. Open package.json in your editor and add "start": "node app.js" inside the scripts block. Save the file.
  6. Create the entry file. Make a file named app.js in the project root. It will import Express, configure the view engine, mount routes, and listen on a port.
  7. Set up the view engine. Install a template engine with npm install ejs. In app.js, set app.set('view engine', 'ejs') and app.set('views', './views'). EJS lets you embed JavaScript in HTML templates.
  8. Write a model. Create models/itemModel.js. Export an array of items and functions such as getAll() and getById(id). Keeping data access here separates it from routing and display.
  9. Write a controller. Create controllers/itemController.js. Import the model and export functions like list and detail that call the model and render a view with the data.
  10. Write views. In views, create index.ejs and detail.ejs. Use EJS tags such as <%= item.name %> to output model data. Keep HTML structure in the views, not in controllers.
  11. Wire routes. In app.js, import the controller and add routes: app.get('/', itemController.list) and app.get('/items/:id', itemController.detail). Also add app.use(express.static('public')) so CSS and images load.
  12. Start the server. Run npm start. The terminal should print a message like Server running on http://localhost:3000. Open that URL in a browser.
  13. Test the routes. Visit / to see the list and /items/1 to see a detail page. If a page errors, read the terminal stack trace; it names the file and line.
  14. Add a form and POST route. Create a form in a view that submits to /items, add app.use(express.urlencoded({ extended: true })), and add app.post('/items', itemController.create). The controller reads req.body and pushes a new item into the model.
  15. Stop and restart cleanly. Press Ctrl+C to stop the server. Install nodemon as a dev dependency with npm install --save-dev nodemon and change the start script to nodemon app.js for automatic restarts during development.

Common Mistakes

  • Putting logic in the wrong layer. Controllers should not contain SQL or file reads, and views should not contain business rules. If a view calls the model directly, move that call into the controller.
  • Forgetting express.urlencoded. Without it, req.body is undefined on form submissions and the POST handler fails silently.
  • Wrong folder paths. app.set('views', './views') is relative to where you run the command. Starting the app from a different directory causes template-not-found errors.
  • Editing files while the server runs without a watcher. Plain node does not reload changes. Restart manually or use nodemon.
  • Port already in use. Another process may hold port 3000. Change the port in app.listen(3000) to 3001 and retry.
  • Committing node_modules. Add a .gitignore containing node_modules before your first commit.

Tips

  • Keep one controller per resource and one model per data type; this scales better than a single large file.
  • Use console.log sparingly and remove it before sharing code. The terminal output is your main debugging tool.
  • Name routes after resources (/items, /items/:id) so the URL structure mirrors your controllers.
  • Store configuration such as the port in environment variables and read them with process.env.PORT.
  • Run npm install after cloning a project; node_modules is not committed.

Troubleshooting

If npm start fails with "Cannot find module", run npm install in the project root. If the browser shows "Cannot GET /", the route is missing or the controller export name is wrong; check the spelling in app.js. If templates fail to render, confirm the file extension matches the view engine and that views is set correctly. If changes do not appear, hard-refresh the browser and restart the server. If the port is busy, stop the other process or change the port. For permission errors on macOS or Linux, avoid sudo npm install; fix ownership of the project folder instead. If you delete package-lock.json accidentally, running npm install regenerates it.

Frequently Asked Questions

Do I need a database to build an MVC app?

No. You can start with an in-memory array or JSON file and swap in a database later by changing only the model layer.

Which framework should I use?

Express with EJS is the smallest setup. Rails, Laravel, Django, and ASP.NET Core follow the same MVC idea with their own CLI commands.

Can I build this on Windows?

Yes. Use Windows Terminal or PowerShell. Folder creation syntax differs slightly, but Node.js and npm commands are identical.

How do I add CSS?

Put stylesheets in the public folder and reference them as /style.css in your views after enabling express.static.

Why does the server stop when I close the terminal?

The process runs inside that terminal session. Closing it ends the process. For persistent hosting, use a process manager or a hosting platform.

Is MVC still relevant?

Yes. Many frameworks still use it because separating data, display, and request handling keeps projects easier to test and maintain.