简体   繁体   English

当我尝试通过连接到// localhost:3000来显示html页面时显示错误,错误被上传为片段

[英]error is being shown when I'm trying to display an html page by connecting to //localhost:3000, the error is uploaded as a snippet

This is the mong.js file which has the code to connect to the mongo server perform get,put,post operations 这是mong.js文件,它具有连接到mongo服务器的代码,执行get,put,post操作

 var express = require('express'); var app = express(); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); app.use(bodyParser.json()); mongoose.connect("mongodb://localhost/test"); var todoschema = new mongoose.Schema ({ name : {type: String, required: true} }); var todomodel = mongoose.model('todolist',todoschema); app.get('/',function(req,res){ res.sendFile('C:\\Users\\Rohit\\Desktop\\New folder\\todo.html'); }) app.get('/todolist', function (req, res){ todomodel.find(function(err,tasks){ res.json(tasks); }); }); app.post('/todolist', function (req, res) { todomodel.insert(req.body, function(err, task) { res.json(task); }); }); app.delete('/todolist/:id', function (req, res) { todomodel.remove(req.params.id, function (err, task) { res.json(task); }); }); app.get('/todolist/:id', function (req, res) { todomodel.findById(req.params.id, function (err, task) { res.json(task); }); }); app.put('/todolist/:id', function (req, res) { todomodel.findAndModify({ query: req.params.id, update: {$set: {name: req.body.name}}, new: true}, function (err, task) { res.json(task); } ); }); app.listen(3000); console.log("Server running on port 3000"); 

This the todo.html file , the todo list page look like this 这是todo.html文件,todo列表页面看起来像这样

 <!DOCTYPE html> <html ng-app="App"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script> <script src="frontend.js"></script> <style> #list { margin-left:320px; font-size: 40px; font-family:verdana; } button { color:yellow;background-color:red;text-align:center;cursor:pointer; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; font-size:40px; padding: 14px 32px; } button:hover { background-color:peachpuff; color:tomato; } </style> </head> <body style="background-color:cyan;"> <div ng-controller="Ctrl"> <h1 style="text-align:center;font-family:verdana;">To-Do LiSt</h1> <div style="margin-left:300px"> <input type="text" ng-model="task.name" style="background-color:black;color:white;font-size:40px;width:40%"> <button ng-click="addtask()">Add</button>&nbsp; <button ng-click="updatetask()">Update</button>&nbsp;<button ng-click="clearfield()">Clear</button> </div> <ul> <li id="list" ng-repeat="task in todolist"> {{task.name}} <button ng-click="deletetask(task._id)">Delete</button>&nbsp;&nbsp;<button ng-click="edittask(task._id)">Edit</button> </tr> </table> </div> </body> </html> 

This is the frontend.js file which has the angular js part and the the functions by which my todo list page works 这是frontend.js文件,它有角度js部分和我的待办事项列表页面工作的函数

 var App = angular.module('App',[]); App.controller('Ctrl',['$scope','$http',function($scope,$http) { var reset = function(){ $http.get('/todolist').success(function(response){ $scope.todolist=response; $scope.task=""; }); }; reset(); $scope.addtask = function() { $http.post('/todolist', $scope.task).success(function(response) { reset(); }); }; $scope.deletetask = function() { $http.delete('/todolist/'+ id).success(function(response){ reset(); }); }; $scope.edittask = function(id) { $http.get('/todolist/'+ id).success(function(response){ $scope.task=response; }); }; $scope.updatetask = function(id){ $http.put('/todolist/'+$scope.task._id, $scope.task).success(function(response){ reset(); }); }; $scope.clearfield = function(){ $scope.task=""; } }]); 

 TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (C:\\Users\\Rohit\\node_modules\\express\\lib\\response.js:403:11) at C:\\Users\\Rohit\\Desktop\\New folder\\mong.js:17:9 at Layer.handle [as handle_request] (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\layer.js:95:5) at next (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\route.js:131:13) at Route.dispatch (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\route.js:112:3) at Layer.handle [as handle_request] (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\layer.js:95:5) at C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:277:22 at Function.process_params (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:330:12) at next (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:271:10) at jsonParser (C:\\Users\\Rohit\\node_modules\\body-parser\\lib\\types\\json.js:100:40) at Layer.handle [as handle_request] (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\layer.js:95:5) at trim_prefix (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:312:13) at C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:280:7 at Function.process_params (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:330:12) at next (C:\\Users\\Rohit\\node_modules\\express\\lib\\router\\index.js:271:10) at expressInit (C:\\Users\\Rohit\\node_modules\\express\\lib\\middleware\\init.js:33:5) 

I'm trying to display my html file when i open http://localhost:3000 , but I'm not able to, Can you please guide me on how to do it , I'm new to all this , An easier solution would be more helpful so that I could understand it. 当我打开http:// localhost:3000时 ,我正在尝试显示我的html文件,但我无法,请你指导我如何做到这一点,我是新手,这是一个更简单的解决方案会更有帮助,这样我才能理解它。

Use: 采用:

res.sendFile('./todo.html');

OR 要么

var path = require('path');
res.sendFile(path.join(__dirname + '/todo.html'));

Second ones better. 第二个更好。

But again your frontend.js file would not load. 但是再次你的frontend.js文件无法加载。 So, best approach is to make a public folder and serve the static files using express.static middleware. 因此,最好的方法是使用express.static中间件创建一个公用文件夹并提供静态文件。

app.use(require('express').static('./public'));

for further explanation you can refer to: https://stackoverflow.com/a/36041093/2680461 有关进一步说明,请参阅: https//stackoverflow.com/a/36041093/2680461

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到http:// localhost:3000 / api / contact时出错 - there was error connecting to http://localhost:3000/api/contact 当我想将 localhost:4000 重定向到 localhost:3000 时出现错误 CORS - Error CORS when i want redirecting localhost:4000 to localhost:3000 当我尝试打开http:// localhost:3000 / pages / home时出错 - Error when I try to open http://localhost:3000/pages/home 当我尝试在端口 5500 上启动 localhost 时,Google Chrome 无法连接 - Google Chrome is stuck connecting when I'm trying to launch localhost on port 5500 尝试将 Motoko 片段添加到 edX 页面,但仅解析 HTML - Trying to add a Motoko snippet to an edX page and only the HTML is being parsed 使用 vanilla spa 时出现错误 router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found) - when use vanilla spa i have an error router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found) 当我尝试打开“update_item”页面时,我不断收到此错误 - I keep getting this error when I'm trying to open the 'update_item' page 尝试在 localhost:3000 上的后端和 localhost:8080 上的前端之间建立 socket.io 连接时出现 CORS 错误 - Getting a CORS error when trying to establish socket.io connection between back-end on localhost:3000 and front-end on localhost:8080 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it 使用getJSON函数抓取网页HTML尝试将调用的内容保存到变量时出现错误 - Using a getJSON function to scrape webpage HTML I'm getting an error when trying to save the contents of the call to a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM