(cd lesson02/http-server
)
-
npm init y
-
Create root file
app.js
-
Package
nodemon
asdevDependencies
-npm install --save-dev nodemon
- Add scripts in
package.json
:
"start": "node app.js",
"start:dev": "nodemon app.js"
-
Import modules
http
,fs
,path
inapp.js
-
Create TypeMime-object:
const TypeMime = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".png": "image/png",
".ico": "image/x-icon",
};
-
Create function
requestHandler
with optionsrequest
andresponse
-
Create variable
server
and use the http-module's methodhttp.createServer(requestHandler)
-
Assign port:
const PORT = process.env.PORT || 3000;
-
Raise the server:
server.listen(PORT, (error) => {
if(error) {
console.error('...')
}
console.log('...')
});
-
npm run start
ornpm run start:dev
-
Go to
http://localhost:3000/
(cd lesson02/express-server-0
)
-
npm i express
-
Create root file
server.js
-
Add scripts in
package.json
:
"start": "node server.js",
"start:dev": "nodemon server.js"
-
Do import module
express
inserver.js
-
Initialization express-app:
const app = express();
- Assign port:
const PORT = 8081;
- Raise the server:
app.listen(PORT, (err) => {
if(err) {
console.error('...')
}
console.log('...')
});
- Define routes (example - get):
app.get('/home', (req, res) => {
res.send('get request');
});
- Define routes (example - post, delete, put, use(all methods)):
app.post('/home', (req, res) => {
res.send('post request');
});
app.delete('/home', (req, res) => {
res.send('delete request');
});
app.put('/home', (req, res) => {
res.send('put request');
});
app.use((req, res) => {
res.send('middleware request');
...
})
- Create example custom Middleware
// Example custom Middleware
// app.use((req, res, next) => {
// console.log(`${req.method} ${req.originalUrl}, ${new Date().toISOString()}`);
// next();
// });
// app.get("/home", (req, res) => {
// res.json({ javascript: "object" });
// });
- Create example Express Middlewares
// Example Express Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // for html-forms
app.use(express.static("public")); // for public-directory files
app.post("/home", (req, res) => {
if (!req.body.goit) {
return res.status(400).json({ status: "goit parameter is required" });
}
res.json({ javascript: "object", body: req.body });
});
- Create example npm Middleware (morgan for log):
-
npm i morgan
-
do import module
morgan
inserver.js
-
// Example npm Middleware (morgan) app.use(morgan("tiny")); // for log
-
npm run start
ornpm run start:dev
-
Go to
http://localhost:8081/
(cd lesson02/express-server
)
-
npm init y
-
Create root file
app.js
-
Package
nodemon
asdevDependencies
-npm install --save-dev nodemon
- Add scripts in
package.json
:
"start": "node app.js",
"start:dev": "nodemon app.js"
-
npm i express
-
Import modules
express
,fs
,path
inapp.js
-
Initialization express-app:
const app = express();
- Assign port:
const PORT = process.env.PORT || 3000;
- Raise the server:
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
});
-
Use methods routs
-
Use ejs-package template (
npm i ejs
) -
npm run start
ornpm run start:dev
-
Go to
http://localhost:3000/