Skip to content

Instantly share code, notes, and snippets.

@gokaybiz
Last active March 4, 2019 18:19
Show Gist options
  • Save gokaybiz/e249de4be95083e2890330fc928146d0 to your computer and use it in GitHub Desktop.
Save gokaybiz/e249de4be95083e2890330fc928146d0 to your computer and use it in GitHub Desktop.
Simple nodejs http server and api request
let log = console.log,
port = process.env.port || 8081
const http = require("http"),
web = http.createServer(
(req, res) => {
log(`Istek geldi: |${req.method}| ${req.url}`)
if (req.url == '/uzat') {
let todos = http.request({
host: "jsonplaceholder.typicode.com",
path: "/todos/1"
}, (resp) => {
resp.setEncoding('utf8')
resp.on('data',
(chunk) => {
res.setHeader('content-type', 'application/json')
res.write(chunk)
res.end()
}
)
}
)
todos.end()
} else {
res.setHeader('content-type', 'text/html; charset=utf-8');
res.write("go fuck yourself anam? <a href" + '="/uzat"> ' + "TIKLA</a>")
res.end()
}
}
)
web.listen(port,
() => {
log(`http://127.0.0.1:${port} adresinde calisiyor anam!`)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment