ReferenceError: require is not defined site:stackoverflow.com code example
Example: 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();
});