Tutkitaan Express.js sovellusta ja http-metodien käyttöä sovelluksessa
- Luo kansio nimeltään expressExample ja avaa komentokehote kansioon
- Alusta node.js sovellus komennolla npm init
- Lisää sovellukseen Express.js moduuli komennolla npm install express
- Lisää sovellukseen tiedosto app.js, jossa koodi
const express=require('express');
const port=3000;
const app=express();
app.listen(port, function(){
console.log("Express esimerkki portissa "+port);
});
app.get('/',function(request,response){
response.send("Express API esimerkki ilman tietokantaa portissa "+port);
});
module.exports=app;
- Käynnistä sovellus komennolla node app.js ja avaa selaimeen sivu http://localhost:3000
- Kokeile muokata tekstiä, joka tulostuu selaimeen. Huomaa, että sinun tulee sammuttaa sovellus (CTRL+C) ja käynnistää se uudelleen
- Asenna koneellesi nodemon apuohjelman komennolla npm install nodemon -g
- Sammuta sovellus ja käynnistä se komennolla nodemon app.js. Kokeile muokata tekstiä ja huomaat, että sovellus käynnistyy automaattisesti uudelleen aina kun tallet JavaScript tiedoston
- Lisää package.json tiedoston script lohkoon rivi:
"start":"node app.js"
- Voit nyt käynnistää sovelluksen komennolla npm start
- Lisää tiedostoon toinenkin get-pyyntöihin vastaava metodi
- Lisää tiedostoon get-pyyntöihin vastaava metodi, joka ottaa vastaan annetun nimen ja palauttaa tervehdyksen
- Lisää sovellukseen rivit
app.use(express.json());
app.use(express.urlencoded({extended:false}));
- Lisää sovellukseen post-pyyntöihin vastaava metodi, joka palauttaa kaiken sille pyynnön body-osassa annetun datan
- Testaa sovellusta Postmanilla
- Kokeile middleware funktioiden toimintaa
Exploring an Express.js application and using HTTP methods in the application.
- Create a folder named expressExample and open a command prompt in the folder.
- Initialize a Node.js application with the command:
npm init
- Add the Express.js module to the application with the command:
npm install express
- Create a file named app.js in the application and add the following code:
const express = require('express'); const port = 3000;
const app = express(); app.listen(port, function(){ console.log("Express example running on port " + port); });
app.get('/', function(request, response){ response.send("Express API example without a database running on port " + port); });
module.exports = app;
- Start the application using the command:
node app.js
Then, open your browser and navigate to: http://localhost:3000 - Try modifying the text displayed in the browser. Note that you need to stop the application (CTRL+C) and restart it for the changes to take effect.
- Install the nodemon utility with the command:
npm install nodemon -g
- Stop the application and restart it with:
nodemon app.js
Now, try modifying the text again—you will notice that the application restarts automatically whenever you save the JavaScript file. - Add the following line to the "scripts" section in the package.json file:
"start": "node app.js"
You can now start the application using the command: npm start
- Add another method to handle GET requests in the file.
- Add a GET request handler that accepts a name parameter and returns a greeting.
- Include the following lines in the application:
app.use(express.json()); app.use(express.urlencoded({ extended: false }));
- Add a method to handle POST requests that returns all data received in the request body.
- Test the application using Postman.
- Experiment with middleware functions.