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?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line