Speed up development with full-stack environments for every branch.

Learn More

node project [Node.js and Hello World]

Forked from Hello World for Node.js.

18 Runs 26 Views 21 Copies

Hello world in Node.js

Saved

Saved

danpillay87 3

danpillay87
published 4 months ago

    'use strict';
    
    const express = require('express');
    const bodyParser = require('body-parser');
    const https = require('https');
    
    const restService = express();
    
    var url = require('url');
    
    function fullUrl(req) {
      return url.format({
        protocol: req.protocol,
        host: req.get('host'),
        pathname: req.originalUrl
      });
    }
    
    restService.use(bodyParser.urlencoded({
    extended: true
    }));
    
    restService.use(bodyParser.json());
    
    restService.post('/hook', function(req, res) {
    //var speech = req.body.result && req.body.result.parameters && req.body.result.parameters.echoText ? req.body.result.parameters.echoText : "Seems like some problem. Speak again."
    var speech= req.body.result && req.body.result.parameters && req.body.result.parameters.leads ? "talking about the leads boss?" : "Seems like some problem. Speak again."
    getSalesData(leadsCallBack, speech, res);
    });
    
    function leadsCallBack(leads, speech, res){
    console.log(leads);
    return res.json({
    speech: speech + leads,
    displayText: speech + getSalesData(),
    source: 'emma-sales'
    });
    }
    
    //get things from the API
    function getSalesData(callBack, speech, res) {
    var https = require('https');
    var url = 'https://demo6357459.mockable.io/googlehome/action/';
    
    https.get(url, function(res){
    var body = '';
    
    res.on('data', function(chunk){
    body += chunk;
    });
    
    res.on('end', function(){
    var apiResponse = JSON.parse(body);
    var leads = apiResponse.leads;
    callBack(leads, speech, res)
    });
    }).on('error', function(e){
    console.log("Got an error: ", e);
    });
    }
    
    restService.listen((process.env.PORT || 8000), function() {
    console.log("Server up and listening");
    });
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).