ReferenceError: require is not defined site:stackoverflow.com

const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end('Am I really running a server?!');
}).listen(8080, '127.0.0.1');

console.log('running server!');


// Creat port 
const port = 8080;

// Setup empty JS object to act as endpoint for all routes
projectData = {};

// Require Express to run server and routes
const app = express();
// Start up an instance of app

/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Cors for cross origin allowance
app.use(cors());

// Initialize the main project folder
app.use(express.static('website'));


// Setup Server 
app.listen(port, () => {
    console.log(`server working on : http://localhost:${port}...`)
});
// Express to run server and routes
// // Callback function to complete GET '/all' by http://localhost:8080/all 
app.get('/all', (req, res) => {
    res.send(projectData).status(200).end();
});

// Post Route all by http://localhost:8080/postAll 
app.post('postAll', (req, res) => {
    // Post Data 
    projectData = {
        temp: req.body.temp,
        date: req.body.date,
        content: req.body.content
    };
    res.send(projectData).status(200).end();
});

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source