Node.jsでのサーバー作成
Q&A
Closed
Node.jsでのサーバー作成
プログラミング初心者です。Node.jsの入門書を見ながら勉強をしていて、Webサーバーの作成という項目を学んでいます。入門書通りにコードを書いたつもりですが、エラーが出てしまいました。
listenメソッドでエラーが出ているのですがどこかコードの間違いがあれば教えていただきたいです。
環境
OS:windows10 Node.jsバージョン:v14.17.0
サーバー作成のコード
main.js
const port = 3000,
http = require('http'),
httpStatus = require('http-status-codes'),
app = http.createServer = ((request, response) => {
console.log('Received an incoming request!');
response.writeHead(httpStatus.OK, {
'Content-Type': 'text/html; charset=utf-8'
});
let responseMessage = '<h1>Hello, Universe!</h1>';
response.write(responseMessage);
response.end();
console.log(`Sent a response : ${responseMessage}`);
});
app.listen(port);
console.log(`The server has started and is listening on port number: ${port}`);
実行結果
$ node main.js
D:\simple_server\main.js:17
app.listen(port);
^
TypeError: app.listen is not a function
at Object.<anonymous> (D:\simple_server\main.js:17:5)
?[90m at Module._compile (internal/modules/cjs/loader.js:1068:30)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:933:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:774:14)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)?[39m
?[90m at internal/main/run_main_module.js:17:47?[39m
0