简体   繁体   中英

Im trying to test out node.js

so I just downloaded and installed node/npm and attempted to run a test with the following code.

const http = require(‘http’);

const hostname = ‘127.0.0.1’;
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello from NodeJS\n’);
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

but the output gives me plenty of error messages (an identifier cant go after this identifier, this cant go here, unexpected, etc) I see a couple of things that could be wrong, but I'm new to this.

You are using some strange characters, this: '

Try to use " or ' in module import, setHeader, ...

I would direct you to https://nodejs.dev/learn

You need to require the http module by doing const http = require('http');

Also your quotation marks are improper they must be single quotes, double quotes, or backticks. They cannot be whatever unicode character you have in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM