Build an MVC Web App from the Terminal: Step-by-Step
In this article
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
- Verify your tools. In the terminal run
node -vandnpm -v. You should see version numbers. If not, install Node.js LTS from the official site and reopen the terminal. - Create the project folder. Run
mkdir mvc-app && cd mvc-app(on PowerShell usemkdir mvc-app; cd mvc-app). Then runnpm init -yto createpackage.json. - Install Express. Run
npm install express. This adds Express tonode_modulesand records it as a dependency. - 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. - Add a start script. Open
package.jsonin your editor and add"start": "node app.js"inside thescriptsblock. Save the file. - Create the entry file. Make a file named
app.jsin the project root. It will import Express, configure the view engine, mount routes, and listen on a port. - Set up the view engine. Install a template engine with
npm install ejs. Inapp.js, setapp.set('view engine', 'ejs')andapp.set('views', './views'). EJS lets you embed JavaScript in HTML templates. - Write a model. Create
models/itemModel.js. Export an array of items and functions such asgetAll()andgetById(id). Keeping data access here separates it from routing and display. - Write a controller. Create
controllers/itemController.js. Import the model and export functions likelistanddetailthat call the model and render a view with the data. - Write views. In
views, createindex.ejsanddetail.ejs. Use EJS tags such as<%= item.name %>to output model data. Keep HTML structure in the views, not in controllers. - Wire routes. In
app.js, import the controller and add routes:app.get('/', itemController.list)andapp.get('/items/:id', itemController.detail). Also addapp.use(express.static('public'))so CSS and images load. - Start the server. Run
npm start. The terminal should print a message likeServer running on http://localhost:3000. Open that URL in a browser. - Test the routes. Visit
/to see the list and/items/1to see a detail page. If a page errors, read the terminal stack trace; it names the file and line. - Add a form and POST route. Create a form in a view that submits to
/items, addapp.use(express.urlencoded({ extended: true })), and addapp.post('/items', itemController.create). The controller readsreq.bodyand pushes a new item into the model. - Stop and restart cleanly. Press Ctrl+C to stop the server. Install
nodemonas a dev dependency withnpm install --save-dev nodemonand change the start script tonodemon app.jsfor 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.bodyis 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
nodedoes 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.gitignorecontainingnode_modulesbefore 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.logsparingly 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 installafter cloning a project;node_modulesis 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.