Giter Site home page Giter Site logo

backendtask's Introduction

Deploying the Time and Date function on node.js server(Local Host), Using VSCode

Creating a basic http server

We'll be creating a server that returns the present time and date to the user, this server would be run on our localhost on port 3000.

Step1:

We start by making our directory and creating the file that will house the code

Screenshot (164)

Step2:

We load the http module that’s standard with all Node.js installations. Add the following line to hello.js

Screenshot (157)

The http module, contains the function to create the server.

Step3:

We define 2 constant that our server would be bound to, the host and the port respectively. The value localhost is a special private address that computers use to refer to themselves, and it's available to the local computer, not just any local network, or to the internet. The port is a number that servers use as an endpoint or “door” to our IP address. When we bind our server to this host and port, we will be able to reach our server by visiting http://localhost:3000 in a local browser.

Screenshot (158)

Step4:

We create the function that would return the present time and date when we start the server.

Screenshot (159)

Step5:

Now we can create our server, and make use of our request listener function.

  • All request listener functions in Node.js accept two arguments: req and res (we can name them differently if we want).
  • The HTTP request the user sends is captured in a Request object, which corresponds to the first argument, req.
  • The HTTP response that we return to the user is formed by interacting with the Response object in second argument, res.

Screenshot (160)

  • In the first line, we create a new server object via the http module’s createServer() function. This server accepts HTTP requests and passes them on to our request Listener function.

  • The last line of the function, res.end(clock);, writes the HTTP response back to the client who requested it. This function returns any data the server has to return. In this case, it’s returning Date and Time function, which is called by "clock".

Step6:

  • After we create our server, we must bind it to a network address, We do that with the server.listen() method. It accepts three arguments: port, host, and a callback function that fires when the server begins to listen. Although all of these arguments are optional, it is usually a good idea to explicitly state which port and host we want a web server to use.

Screenshot (161)

Now we have our server, let's see it in action and test it by running our server. In the console we should see this output

Screenshot (162)

When we click on the link it opens up our web browser to this page

Screenshot (163)

After confirming the server runs the D&T function, we can stop the server process with ctrl + c

backendtask's People

Contributors

strangejay avatar

Stargazers

Ehigiator Akhile avatar

Watchers

 avatar

backendtask's Issues

Testing

Outlook sink test, hello world if this comes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.