Reputation: 63
Trying to follow a sample and I can't figure out why there are errors. Any help...it's probably a formatting thing:
import http from 'http';
import express from 'express';
// Express app setup
const app = express();
const server = http.createServer(app);
server.listen(3000);
server.on('listening', () => {
console.log('Server is listening on port: 3000');
app.get('*', (req, res) => {
res.end('Hello Express');
});};
My two error messages are:
Parsing error: Unexpected token, expected ","
9 | app.get('*', (req, res) => { 10 | res.end('Hello Express');
11 | });}; | ^
')' expected.
Upvotes: 0
Views: 41
Reputation: 544
There’s a few issues here:
The import
syntax is not valid in nodejs unless you have a transpiler to intercept it.
Your setup with express is just wrong. You’re defining your route handlers after you’ve started the server.
Here’s what you want - with valid common js syntax
const http = require('http')
const app = require('express')()
app.get('*', (req, res) => res.send(' Hello Express'))
const server = http.createServer(app)
server.listen(3000, () => console.log('Server is listening on port: 3000'))
Upvotes: 1