我正在尝试创建一个简单的快速应用程序,但似乎没有工作
我正在尝试创建一个简单的快速应用程序,但它在控制台中运行。当我在浏览器中点击localhost:3000时出现网络错误。我似乎不知道问题是什么。
这是我的代码。
var hostname = 'localhost';
var port = 3000;
var app = express ();
app.use (function (req, res, next) {
console.log (req.headers);
res.writeHead (200, {'Content-Type': 'text/html'});
res.end ( '<html><body><h1>Hello world</h1></body></html>');
});
// listing for request at port: 7000 no http.createServer needed
app.listen (console.log (
`Success server running at http://${hostname}: ${port}`
));
然而,当我在纯节点中创建一个类似的应用程序时,它运行正常
这是我的代码:
var fs = require('fs');
var path = require ('path');
var http = require ('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer (function (req, res) {
console.log ('request for ' + req.url + ' using ' + req.method + ' method');
// checking if the request method is Get
if (req.method == 'GET') {
var fileUrl;
// checking for the request url if it is the home page or not and storing the correct request url in fileUrl variable
if (req.url == '/') fileUrl = '/index.html';
else fileUrl = req.url;
var filePath = path.resolve ('./public'+fileUrl);
var fileExt = path.extname (filePath);
if (fileExt == '.html' && req.url !== '/favicon.ico') {
fs.exists (filePath, function (exists) {
if (!exists) {
res.writeHead (404, {'content-type': 'text/html'});
res.end ('<h1> The file </h1>' + fileUrl + '<h1>is not found. Make sure your browser input is correct and try again!</h1>');
console.log('hello no favicon found');
return;
}
});
}
res.writeHead (200, {'content-type': 'text/html'});
fs.createReadStream (filePath).pipe(res);
} else {
res.writeHead (404, {'content-type': 'text/html'});
res.end ('<h1> The file' + fileUrl + 'not an html file');
console.log (fileUrl);
}
});
server.listen (port, hostname, function (){
console.log (`server started ${hostname}:${port}. Helloooooo`);
});
感谢您的评论和回复!
回答如下:您的代码中存在几个问题。
您应该在项目中安装express
。您可以使用以下命令执行此操作。
npm install express --save
然后在顶部使用以下代码。
var express = require('express');
var app = express();
然后,您必须在listen
调用中提供port参数。
app.listen(3000);
其他API和选项可以找到here。
我正在尝试创建一个简单的快速应用程序,但似乎没有工作
我正在尝试创建一个简单的快速应用程序,但它在控制台中运行。当我在浏览器中点击localhost:3000时出现网络错误。我似乎不知道问题是什么。
这是我的代码。
var hostname = 'localhost';
var port = 3000;
var app = express ();
app.use (function (req, res, next) {
console.log (req.headers);
res.writeHead (200, {'Content-Type': 'text/html'});
res.end ( '<html><body><h1>Hello world</h1></body></html>');
});
// listing for request at port: 7000 no http.createServer needed
app.listen (console.log (
`Success server running at http://${hostname}: ${port}`
));
然而,当我在纯节点中创建一个类似的应用程序时,它运行正常
这是我的代码:
var fs = require('fs');
var path = require ('path');
var http = require ('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer (function (req, res) {
console.log ('request for ' + req.url + ' using ' + req.method + ' method');
// checking if the request method is Get
if (req.method == 'GET') {
var fileUrl;
// checking for the request url if it is the home page or not and storing the correct request url in fileUrl variable
if (req.url == '/') fileUrl = '/index.html';
else fileUrl = req.url;
var filePath = path.resolve ('./public'+fileUrl);
var fileExt = path.extname (filePath);
if (fileExt == '.html' && req.url !== '/favicon.ico') {
fs.exists (filePath, function (exists) {
if (!exists) {
res.writeHead (404, {'content-type': 'text/html'});
res.end ('<h1> The file </h1>' + fileUrl + '<h1>is not found. Make sure your browser input is correct and try again!</h1>');
console.log('hello no favicon found');
return;
}
});
}
res.writeHead (200, {'content-type': 'text/html'});
fs.createReadStream (filePath).pipe(res);
} else {
res.writeHead (404, {'content-type': 'text/html'});
res.end ('<h1> The file' + fileUrl + 'not an html file');
console.log (fileUrl);
}
});
server.listen (port, hostname, function (){
console.log (`server started ${hostname}:${port}. Helloooooo`);
});
感谢您的评论和回复!
回答如下:您的代码中存在几个问题。
您应该在项目中安装express
。您可以使用以下命令执行此操作。
npm install express --save
然后在顶部使用以下代码。
var express = require('express');
var app = express();
然后,您必须在listen
调用中提供port参数。
app.listen(3000);
其他API和选项可以找到here。