express webserver in javascript

To create a basic web server in JavaScript using Node.js, you can use the built-in http module. Here's an example:

const http = require('http');
const port = 3000;

const requestHandler = (request, response) => {
  console.log(request.url);
  response.end('Hello Node.js Server!');
};

const server = http.createServer(requestHandler);

server.listen(port, (err) => {
  if (err) {
    return console.log('Error:', err);
  }

  console.log(`Server is listening on ${port}`);
});
363 chars
18 lines

In this example, we create a server on port 3000 and define a request handler function that logs the URL of any incoming requests and sends a response back to the client.

You can run this code in your terminal with the node command, and then visit http://localhost:3000 in a web browser to see the response.

gistlibby LogSnag