Answers for "node js expres"

8

express js 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}`));
Posted by: Guest on September-29-2020
3

node js express

// node js -> express -> basic example: static folder, 404 page

const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;

const app = express();

function error404(req, res) {
  res.status(404);

  if (req.accepts('html')) {
    res.sendFile(path.join(__dirname, 'public/errors/404.html'));
    return;
  }

  if (req.accepts('json')) {
    res.send({
      status: 404,
      error: 'Not found'
    });
    return;
  }
  
  res.type('txt').send('404 - Not found');
}

app
  .use(express.static(path.join(__dirname, 'public')))
  .use(error404)
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));
Posted by: Guest on June-07-2020
0

express in node js

$ npm init --y //add the package.json file

$ npm install express --no-save

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

//cors to fix cors origin, body-parser to fix the post value on the server
const cors = require('cors');
const bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json());

const port = 3000

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

router.get('/admin/:id?', (req, res) => {
  let id = req.params.id;
}

app.post('/admin', (req, res) => {
  const user = req.body.user;
  res.send('Hello World!', user)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
Posted by: Guest on August-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language