Open 3bdoYasser opened 11 months ago
CREATE package.json
{
"name": "simple-test",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
CREATE app.js
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`);
});
CREATE controllers/testController.js
exports.test = (req, res) => {
res.send('Hello World!');
};
CREATE routes/testRoute.js
const express = require('express');
const testController = require('../controllers/testController');
const router = express.Router();
router.get('/', testController.test);
module.exports = router;
UPDATE app.js
const express = require('express');
const testRoute = require('./routes/testRoute');
const app = express();
const port = 3000;
app.use('/', testRoute);
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`);
});
This structure follows the MVC (Model-View-Controller) architecture, where testController.js
acts as the controller. In a full-fledged application, we would also have models (for database interactions) and views (for rendering data to the user). However, for this simple test, we only need a controller to handle the routing logic.
create nodejs project with MVC architecture.