REST API with Express.js
Express Example
Tutkitaan Express.js sovellusta ja http-metodien käyttöä sovelluksessa
  1. Luo kansio nimeltään expressExample ja avaa komentokehote kansioon
  2. Alusta node.js sovellus komennolla npm init
  3. Lisää sovellukseen Express.js moduuli komennolla npm install express
  4. 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;
    
  5. Käynnistä sovellus komennolla node app.js ja avaa selaimeen sivu http://localhost:3000
  6. Kokeile muokata tekstiä, joka tulostuu selaimeen. Huomaa, että sinun tulee sammuttaa sovellus (CTRL+C) ja käynnistää se uudelleen
  7. Asenna koneellesi nodemon apuohjelman komennolla npm install nodemon -g
  8. 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
  9. Lisää package.json tiedoston script lohkoon rivi:
    "start":"node app.js"
    
  10. Voit nyt käynnistää sovelluksen komennolla npm start
  11. Lisää tiedostoon toinenkin get-pyyntöihin vastaava metodi
  12. Lisää tiedostoon get-pyyntöihin vastaava metodi, joka ottaa vastaan annetun nimen ja palauttaa tervehdyksen
  13. Lisää sovellukseen rivit
    app.use(express.json());
    app.use(express.urlencoded({extended:false}));
    
  14. Lisää sovellukseen post-pyyntöihin vastaava metodi, joka palauttaa kaiken sille pyynnön body-osassa annetun datan
  15. Testaa sovellusta Postmanilla
  16. Kokeile middleware funktioiden toimintaa

Exploring an Express.js application and using HTTP methods in the application.
  1. Create a folder named expressExample and open a command prompt in the folder.
  2. Initialize a Node.js application with the command:
    npm init
  3. Add the Express.js module to the application with the command:
    npm install express
  4. 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; 
  5. Start the application using the command:
    node app.js
    Then, open your browser and navigate to: http://localhost:3000
  6. 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.
  7. Install the nodemon utility with the command:
    npm install nodemon -g
  8. 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.
  9. 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
  10. Add another method to handle GET requests in the file.
  11. Add a GET request handler that accepts a name parameter and returns a greeting.
  12. Include the following lines in the application:
     app.use(express.json()); app.use(express.urlencoded({ extended: false })); 
  13. Add a method to handle POST requests that returns all data received in the request body.
  14. Test the application using Postman.
  15. Experiment with middleware functions.



Toggle Menu