nodejs express server example

Example 1: simple express server

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('<h1>Some HTML</h1>');
  res.send('<p>Even more HTML</p>');
});

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));

Example 2: node.js express

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

Example 3: node js express

basic server

const express =require('express');
const app = express();
const PORT = 5000;


app.get('/',(req,res)=>{
   res.json({message: 'Welcome to the backend'})
})


app.listen(PORT ,()=>console.log(`Connected to ${PORT}`)

Example 4: require express server.js

const express = require('express')const app = express() app.get('/', function (req, res) {  res.send('Hello World')}) app.listen(3000)

Example 5: js express server

const http = require('http')
const express = require('express')

const app = express()
const server = http.Server(app)
app.set('port', 8888)
server.listen(8888)

app.get('/', (req, res) => {
  res.json({teste: true})
})

Tags:

Html Example