Question

In: Computer Science

I am trying to use express to get and post time from a url browser. Here...

I am trying to use express to get and post time from a url browser. Here is my code:

var express = require('express');
var app = express();
var url = require('url');

function parsetime (time) {
return {
hour: time.getHours(),
minute: time.getMinutes(),
second: time.getSeconds()
};
}

function unixtime (time) {
return { unixtime : time.getTime() };
}

app.get('/', function (req, res) {
var parsedUrl = url.parse(req.url, true);
var time = new Date(parsedUrl.query.iso);
var result;

if (/^\/api\/parsetime/.test(req.url))
result = parsetime(time);
else if (/^\/api\/unixtime/.test(req.url))
result = unixtime(time);

if (result) {
console.log(200, 'OK Success');
res.end(JSON.stringify(result));
} else {
console.log(404, 'Page not found');
res.end();
}
})
app.listen(3000);

however I am getting an error

Solutions

Expert Solution

it is not res.end but it is res.send() fundtion to send response back to client

and another this i have tested the endpoint and also added demo test point for just checking pupose

it is working and please do check my comment

var express = require('express');

var app = express();

var url = require('url');

function parsetime(time) {

    return {

        hour: time.getHours(),

        minute: time.getMinutes(),

        second: time.getSeconds()

    };

}

function unixtime(time) {

    return { unixtime: time.getTime() };

}

app.get('/', function (req, res) {

    console.log("in get ////")

    var parsedUrl = url.parse(req.url, true);

    var time = new Date(parsedUrl.query.iso);

    var result;

    // an not sure what it test method doing but it is not going to

    // if block it always goinf to else

    if (/^\/api\/parsetime/.test(req.url))

        result = parsetime(time);

    else if (/^\/api\/unixtime/.test(req.url))

        result = unixtime(time);

    console.log("result = "+result);

    if (result) {

        console.log(200, 'OK Success');

        // send with status code

        res.send(200, JSON.stringify(result));

    } else {

        console.log(404, 'Page not found');

        // send the json response

        res.send(404, {"msg":'page not found'});

    }

})

// test endpoint added you can test it by http://localhost:3000/test

app.get('/test', function(req, res){

    res.send(200, {"msg":"Helloo"});

})

app.listen(3000);

console.log('Server started on localhost:3000.......');

// OUTPUT

Please do let me know if u have any concern...


Related Solutions

I am trying to integrate a Singleton Pattern into this code I had previously made. Here...
I am trying to integrate a Singleton Pattern into this code I had previously made. Here is my code to a previous project: radius = float(input("Please enter the radius: ")) area = 3.14 * radius**2; print("The area is ", area, "square units.") For this project it must be coded in python. Here are the instructions for the project for reference. Implementing Design Patterns with Python Software Design Patterns may be thought of as blue prints or recipes for implementing common...
Not sure how to prompt these functions here. I am trying tocreate a holiday shopping...
Not sure how to prompt these functions here. I am trying to create a holiday shopping list. Here are my three requirements'''This function should prompt the user for the names of all the family members that they will be purchasing gifts for this holiday and return those names in a list'''def family_names():names = []return names'''This function takes the names of all family members as a list and asks the user how much money they will spend on each person. These...
Here is the following information on the Cheesecake Factory, I am trying to answer question 1...
Here is the following information on the Cheesecake Factory, I am trying to answer question 1 below. Can you please help me out? Excel assignment. Current Dividend (Source – Yahoo!Finance) $1.28 Required Return (Estimated) 8.9% Current EPS (Source – Yahoo!Finance) $2.14 Current Book Value Per Share (Source – Yahoo!Finance) $13.24 Current EBITDA per share (Source – Yahoo!Finance) $5.25 Current Debt per share è $2.80 Current Cash and Equivalents per share $0.61 Initial Growth Rate for H-Model (Estimated) 9% Terminal Growth...
I am trying to create my own doubly linkedlist class and here is what I have...
I am trying to create my own doubly linkedlist class and here is what I have so far. my addfirst is adding elements forever and ever, and I could use some help. This is in C++. Please help me complete my doubly linkedlist class. the addfirst method is what I would like to complete. #ifndef DLLIST_H #define DLLIST_H #include "node.h" #include using namespace std; template class DLList { private:     Node* head = nullptr;     Node* tail = nullptr;    ...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
Here is a project I am currently working on and I could use some feedback on...
Here is a project I am currently working on and I could use some feedback on how to get started. I calculated the general probabilities for the first two columns of each spreadsheet (successes / total outcomes). I don't want to move forward until I know I'm on the right track but I'm concerned that I've oversimplified the question being asked. I'm also using Excel and am having a hard time finding an appropriate formula for conditional probability. My book...
I know this is multiple questions, I am going to post it six times each time...
I know this is multiple questions, I am going to post it six times each time asking for a different question, but I also know these questions build off each other so I couldn't just post the prompt and one question without the others. Please answer question 4 (or it all :D) An existing industrial wastewater treatment plant fails to treat a new chemical, pentachlorophenol, leaving the facility in its waste stream. A concentration of 100 μg/L pentachlorophenol now passes...
I am using a Macbook and trying to use the terminal and a VIM editor to...
I am using a Macbook and trying to use the terminal and a VIM editor to create program. This program will allow you to enter the costs of each ingredient and product, then prompt for the amount of each ingredient bought (individually by ingredient) and product sold (also individually), and from there show profit or loss, as well as profit on average for each unit sold. There will be say, 2 ingredients to buy and 3 products to sell. Please...
I am trying to solve this problem, but I don't know which test to use: For...
I am trying to solve this problem, but I don't know which test to use: For average risk funds only, test the proportion of funds with Sales Charges is not 50% use α = 0.10. Explain your conclusion. Here is the contingency table with the data: Sales charge Yes No total Risk Low 6 8 14 Avg 42 34 76 High 24 23 47 Total 72 65 137 Context of the problem: Mutual funds are the most common way people...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT