0

server.js

const http = require('http');
const app  = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer();
server.listen(port);

app.js

const express = require('express');
const app = express();
const productRoutes = require('./api/routes/products');
app.use('/products', productRoutes);
module.exports = app;

so when i just run the code node server.js it just keep looping without any result.

4
  • 1
    what do you want to achieve? Commented Jul 25, 2018 at 13:01
  • what do you wanna do with this server? be more specific please Commented Jul 25, 2018 at 13:03
  • How does productRoutes look like? Commented Jul 25, 2018 at 13:27
  • app is used twice, with require('./app') and express() Commented Jul 25, 2018 at 14:29

3 Answers 3

2

Check out this link It gives a bit more detail on how that works.

const http = require('http');
const net = require('net');
const url = require('url');

// Create an HTTP tunneling proxy
const proxy = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
});
proxy.on('connect', (req, cltSocket, head) => {
  // connect to an origin server
  const srvUrl = url.parse(`http://${req.url}`);
  const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
    cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
                    'Proxy-agent: Node.js-Proxy\r\n' +
                    '\r\n');
    srvSocket.write(head);
    srvSocket.pipe(cltSocket);
    cltSocket.pipe(srvSocket);
  });
});

// now that proxy is running
proxy.listen(1337, '127.0.0.1', () => {

  // make a request to a tunneling proxy
  const options = {
    port: 1337,
    hostname: '127.0.0.1',
    method: 'CONNECT',
    path: 'www.google.com:80'
  };

  const req = http.request(options);
  req.end();

  req.on('connect', (res, socket, head) => {
    console.log('got connected!');

    // make a request over an HTTP tunnel
    socket.write('GET / HTTP/1.1\r\n' +
                 'Host: www.google.com:80\r\n' +
                 'Connection: close\r\n' +
                 '\r\n');
    socket.on('data', (chunk) => {
      console.log(chunk.toString());
    });
    socket.on('end', () => {
      proxy.close();
    });
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

This should be in comment not an answer
please provide more details and a direct answer then refer to the link. Imagine the link is no longer valid, this way your answer will last "forever".
0
const http = require('http');
const app  = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer();
server.listen(port);

As far I could extract from code, the thing that is most probably happening in your server.js is that your server is waiting for some request. And you have nothing in your code handle requests.

1 Comment

thanks i just forget to app the parameter over here const server = http.createServer(app);
0

I think you have to call the require function to return the actual router object, try to change this line

from: const productRoutes = require('./api/routes/products');

to: const productRoutes = require('./api/routes/products')();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.