我创建了一个包含 index.html 和 script.js 文件的项目。所以我已经完成了 npm init -y 来获取 package.json。如果我明确双击 index.html 文件,它会正常打开它并按我想要的方式运行,但如果我使用 express as res.sendFile(path.join(__dirname, './index.html')); 它运行不正常。所以我想以这样的方式编写一个启动脚本,它应该直接运行 index.html
i have tried this
router.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
})
But is is not functioning well. It is just rendering raw html in normal function it is asking me for permission to access camera and microphone. But here it asking nothing.
{
"name": "demo",
"version": "1.0.0",
"description": "VideoCall App",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "AkshayK",
"license": "MIT"
}
I want to render index.html when i do npm start.
我为您制作了一个具有相同功能的应用程序。你可以在这里检查。https://github.com/RajatKaushik/Camera-App
Express 是用于处理 http 请求的中间件,您必须从浏览器发出GET请求。一旦您发出请求,您的 Express 应用程序将匹配路径,并在您的情况下使用文件 (index.html) 进行响应。
因此,如果您想在浏览器上传递 index.html 文件,请执行以下步骤:-
node [YOUR_FILE_NAME.js]
(node script.js)运行您的节点应用程序。本地主机:4200/
因为这是您使用 index.html 文件进行响应的路径。
我认为您的相机和麦克风无法正常工作的原因是,启动该代码的代码位于您已包含在 HTML 文件中的某个脚本文件中,<script src="index.js">
但您并未使用 express 传送该文件。
您可以使用以下代码片段来提供所需的静态文件。
app.use(express.static('__dirname' + '/public'), () => {
console.log('Request Recived');
});
其中 public 是您的index.js
文件所在的文件夹。
我为您制作了一个具有相同功能的应用程序。你可以在这里检查。https://github.com/RajatKaushik/Camera-App
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句