我正在考虑使用 hapi.js 教程制作一个简单的 hello world。
我已经安装了hapi:
npm init
npm install hapi --save
- 我收到一大堆包含文件的文件夹
我尝试执行node index.js
,但这给了我错误。因此,我 cd
进入 node_modules
并在运行 node
时遇到另一个错误。我再次 cd
进入 hapi
,但在运行 node index.js
时再次出现错误。我添加了教程中的所有语法。
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
reply('hello world');
}
});
// Start the server
server.start();
不确定我应该在哪里运行index.js
最佳答案
node_modules
文件夹用于存储应用程序的所有依赖项(即express
、hapi
等)。这类似于其他语言中的 lib
(库)文件夹。
当您使用 npm install
下载依赖项时,node_modules
文件夹将放置在项目的根目录下。您的源文件可以放置在根目录或您创建的子文件夹中的任何位置。但是,它们不应该放置在 node_modules
文件夹中,因为它用于外部依赖项。
与其他答案相反,您不限于在根目录中执行程序 - 您也可以在任何子文件夹中执行它。当您运行程序时,如果 Node 在当前目录中找不到 node_modules
文件夹,它将向上移动到父目录,直到找到为止。请参阅 Node 的 modules documentation .
关于node.js - hapi.js 入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29374927/