express minimal bir nodejs frameworkudur
temel yapi
import express from "express";
const app = express();
app.get("/", (req, res) => {
// res.status(200).send("Hello from the server side!");
res
.status(200)
.json({ message: "Hello from the server side!", app: "Natours" });
});
app.post("/", (req, res) => {
res.end("Your message has been arrived to us");
});
const port = 3000;
app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
send ile direkt mesajlar gonderilir. json ile direkt json gonderilir. content-type otomatik ayarlar
app.use(express.json())
Express’te gelen HTTP isteklerinin body’sini JSON olarak parse etmek için kullanılır. Yani, POST/PUT/PATCH gibi isteklerde client’tan gelen JSON veriyi otomatik olarak req.body
içine koyar.
bu sekilde paramslar alinabilir
app.get(`${API_PREFIX}tours/:id`, (req, res) => {
console.log(req.params);