create a server
const http=require("http")
const server=http.createServer((request,response)=>{
const url=request.url
console.log(url)
response.write('hello world')
response.end()
})
server.listen(8080,()=>{
console.log('localhost:8080')
})
Browser debugging of node:
node --inspect --inspect-brk server.js
Node process management tool:
supervisor
nodemon
forever
pm2
Install nodemon globally:
npm i nodemon -g
Start a service with nodemon
nodemon server.js
When the content of server.js changes, there is no need to restart the service, just refresh the page
The browser side displays:
response.write('hello')
response.end()
Display html by default
response.write('<div>hello</div>')
response.writeHead(200,{'content-type':'text/html'})
Displayed as: hello
If you want to display as text:
response.writeHead(200,{'content-type':'text/plain'})
response.write('<div>hello</div>')
response.end()
then display: <div>hello</div>
return json
response.writeHead(200, {'content-type':'application/json'} )
response.write({"name":"henley"})
response.end()
Cross-domain via proxy: