Reputation: 305
I am a learner in Node.js.
Thanks
Upvotes: 0
Views: 89
Reputation: 1987
Express is a framework for creating web servers in Node.js. You don't need a framework to write a node.js server, but a framework like Express makes your programming job much easier.
Whether you use Express or not makes no difference to how you start your node server.
Simple node.js server without Express:
// index.js
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('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
Launch this server by running node index.js
.
Simple node.js server with Express
// index.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Since this application has Express as dependency, you must have Express installed. Run npm i express
to install express.
Again, launch this server with node index.js
.
You'll notice both of these examples start an HTTP server on port 3000. If the computer's firewall allows connections on port 3000, people can access your node.js server at http://<your_domain>:3000 or http://<your_ip_address>:3000.
If port 3000 is blocked by a firewall, another possibility is to proxy your node.js server with something like nginx. This has the added benefit of using nginx's features for the initial connection, such as TLS.
You can also modify the code above to have the node.js server run on port 80. This won't work if the computer already has port 80 in use, for example, if apache, nginx, etc. are already running. Node.js can also accept TLS (https) connections directly as well.
Upvotes: 1
Reputation: 3194
Here you can see the comparison of nodejs frameworks used in production level and their performance.
Upvotes: 0