gpt4 book ai didi

javascript - 语法错误: Unexpected end of input at Module._compile

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:39 25 4
gpt4 key购买 nike

我正在尝试为我的服务器运行以下 Node JS 代码

但它在第 74 行给出错误,但所有代码在第 72 行完成

我的代码是

server.js

var express = require("express"),
app = express(),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler'),
methodOverride = require('method-override'),
hostname = process.env.HOSTNAME || 'localhost',
port = parseInt(process.env.PORT, 10) || 4004,
publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;


MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
if (err) throw err;
console.log("Connected to Database");

//Show homepage
app.get("/", function (req, res) {
res.redirect("/index.html");
console.log("shubham ");
});
app.get("/index/", function (req, res) {
res.redirect("/index.html");
console.log("shubham ");
});

app.get("/search", function (req, res){
console.log("shubham batra");
var pro_name = req.query.name;
var pro_code = req.query.code;
var pro_category = req.query.category;
var pro_brand = req.query.brand;

console.log(name);
console.log(code);
console.log(category);
console.log(brand);



var documen = {name:pro_name, code:pro_code , category:pro_category, brand:pro_brand };

//insert record
db.collection('urlinfo').insert(documen, function(err, records) {
if (err) throw err;
console.log("Record added as "+records[0]._id);
});

//res.send('You sent the name "' + req.body.name + '".');
console.log(res.body);
res.redirect("/index.html");
});
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));

console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);

当我尝试运行此代码时,它给出以下错误:

shubham@shubham-pc:~/Music/server$ nodejs server.js 

/home/shubham/Music/pricesync/server/server.js:74 });
SyntaxError: Unexpected end of input
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3

最佳答案

您没有正确关闭代码块:

MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
// The rest of your code that needs `db` should go here
});

关于javascript - 语法错误: Unexpected end of input at Module._compile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com