Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

express js basic example

//to run : node filename.js
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}`))

//visit localhost:3000
// assuming you have done 1) npm init 2) npm install express
Comment

express example

const http = require('http');
const app = require('./app');

const normalizePort = val => {
  const port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }
  if (port >= 0) {
    return port;
  }
  return false;
};
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

const errorHandler = error => {
  if (error.syscall !== 'listen') {
    throw error;
  }
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port: ' + port;
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges.');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use.');
      process.exit(1);
      break;
    default:
      throw error;
  }
};

const server = http.createServer(app);

server.on('error', errorHandler);
server.on('listening', () => {
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port ' + port;
  console.log('Listening on ' + bind);
});

server.listen(port);
Comment

PREVIOUS NEXT
Code Example
Javascript :: jsonl parser javascript 
Javascript :: crone expression in spring boot 
Javascript :: delete item from array of objects javascript 
Javascript :: loop through an array 
Javascript :: events 
Javascript :: how to compile typescript to javascript es6 
Javascript :: counter react 
Javascript :: method function difference 
Javascript :: JS how to access a class propert 
Javascript :: angular import service 
Javascript :: remove duplicates strig javascript 
Javascript :: ant design form validation in the modal 
Javascript :: fibonacci series javascript using recursion explanation 
Javascript :: what is a heap in javascript 
Javascript :: hi;ight word in textarea javascript 
Javascript :: connect redux 
Javascript :: crypto random string javascript 
Javascript :: react router native back button 
Javascript :: vuejs jwt authentication example 
Javascript :: sweet alert 2 
Javascript :: remove second last element from array javascript 
Javascript :: tinymce for react 
Javascript :: javaScript throw statement 
Javascript :: Javascript first example 
Javascript :: particle js with react 
Javascript :: use next() in node js 
Javascript :: build angular project 
Javascript :: react hook from 
Javascript :: what is heap in javascript 
Javascript :: .default in javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =