< Express

Express - ES6 modulok

Szerző: Sallai András

Copyright © 2024, Sallai András

Szerkesztve: 2024

Licenc: CC Attribution-Share Alike 4.0 International

Web: https://szit.hu

Az .mjs kiterjesztés

{ "scripts": { "start": "nodemon app/index.mjs --watch app" } }
import express from 'express'; const app = express(); const port = 8000; app.get('/', (req, res) => { res.send('működik'); }); app.listen(port, () => { console.log(`Listening on port ${port}`); });

Babel használata

Projekt létrehozása:

npm init -y

Függőségek telepítése:

npm install --save-dev @babel/core
npm install --save-dev @babel/preset-env
npm install --save-dev @babel/register
npm install --save-dev @babel/node

A Babel beállítása:

{ "presets": [ "@babel/preset-env" ] }
import express from 'express'; const app = express(); const port = 8000; app.get('/', (req, res) => { res.send('működik'); }); app.listen(port, () => { console.log(`Listening on port ${port}`); });

node -r @babel/register app

Indítás nodemon alkalmazással:

{ "name": "app01", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon --exec babel-node app --watch app" }, "keywords": [], "author": "", "license": "ISC", "description": "", "dependencies": { "express": "^4.21.2" }, "devDependencies": { "@babel/core": "^7.26.0", "@babel/node": "^7.26.0", "@babel/preset-env": "^7.26.0", "@babel/register": "^7.25.9", "nodemon": "^3.1.7" } }

npm start

Folytatás Bábel-lel

import express from 'express'; import router from './routes'; const app = express(); const port = 8000; app.use(express.json()); app.use('/api',router); app.listen(port, () => { console.log(`Listening on port ${port}`); });
import Router from 'express'; import employeeController from './controllers/employeeController'; const router = new Router(); router.get('/employees', employeeController.index); export default router;
class EmployeeController { index(req, res) { res.send('dolgozók'); } } export default new EmployeeController();