[英]Node/Express: How do I separate server side and client side javascript?
I'm building a JS project to learn new skills, and I'm having trouble delineating between the client-side code and the server-side code.我正在构建一个 JS 项目来学习新技能,但在区分客户端代码和服务器端代码时遇到了麻烦。 The current setup is a node app with ExpressJS as a dependency.
当前设置是一个以 ExpressJS 作为依赖项的节点应用程序。
When I run npm run build && npm run dev
, the base html/css is served as expected, but I get an error in my console from the express script.当我运行
npm run build && npm run dev
时,基本 html/css 按预期提供,但我在控制台中从 express 脚本中收到错误。
main.fb6bbcaf.js:116 Uncaught TypeError: Cannot read property 'prototype' of undefined at Object.parcelRequire.node_modules/express/lib/response.js.safe-buffer (response.js:42) at newRequire (main.fb6bbcaf.js:47) at localRequire (main.fb6bbcaf.js:53) at Object.parcelRequire.node_modules/express/lib/express.js.body-parser (express.js:22) at newRequire (main.fb6bbcaf.js:47) at localRequire (main.fb6bbcaf.js:53) at Object.parcelRequire.node_modules/express/index.js../lib/express (index.js:11) at newRequire (main.fb6bbcaf.js:47) at localRequire (main.fb6bbcaf.js:53) at Object.parcelRequire.js/main.js.express (main.js:1)
main.fb6bbcaf.js:116 Uncaught TypeError: Cannot read property 'prototype' of undefined at Object.parcelRequire.node_modules/express/lib/response.js.safe-buffer (response.js:42) at newRequire (main.fb6bbcaf. js:47) 在 localRequire (main.fb6bbcaf.js:53) 在 Object.parcelRequire.node_modules/express/lib/express.js.body-parser (express.js:22) 在 newRequire (main.fb6bbcaf.js:47) ) 在 localRequire (main.fb6bbcaf.js:53) 在 Object.parcelRequire.node_modules/express/index.js../lib/express (index.js:11) 在 newRequire (main.fb6bbcaf.js:47) 在 localRequire (main.fb6bbcaf.js:53) 在 Object.parcelRequire.js/main.js.express (main.js:1)
For reference, I'm still trying to get the example code from the express documentation to run, so my whole main.js
file looks like this:作为参考,我仍在尝试从 express 文档中获取示例代码以运行,因此我的整个
main.js
文件如下所示:
const express = require('express');
const app = express();
const port = 1234;
app.get('/hello', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
I found a person with a similar issue HERE , but what doesn't click for me (and what I can't seem to find a proper question for) is how to separate the client-side and server-side code in my project.我在这里找到了一个有类似问题的人,但是对我来说没有点击的(而且我似乎找不到合适的问题)是如何在我的项目中分离客户端和服务器端代码。 I get the feeling that I'm trying to use express in code that's being served to the client, but I don't know where else to put it.
我觉得我正在尝试在提供给客户的代码中使用 express,但我不知道该放在哪里。
Is there a best-practice location for server-side code in a project that uses both node and express?在同时使用 node 和 express 的项目中是否有服务器端代码的最佳实践位置?
Error code say you have problem with Express and body-parser modules.错误代码表明您对 Express 和 body-parser 模块有问题。 lets fix this from beginning.
让我们从头开始解决这个问题。
// creating new project through cmd line // 通过 cmd 行创建新项目
1)cd to project directory 1)cd到项目目录
2)create app.js 2)创建app.js
3)npm init -y // to create project 3)npm init -y // 创建项目
4)npm install express body-parser //installing required modules 4)npm install express body-parser //安装需要的模块
//open app.js and configure server using below code //打开app.js并使用以下代码配置服务器
const express = require("express");
const bodyParser = require("body-parser");
let app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/",function(req,res){
res.send("Hello world");
})
app.listen(3000, function() {
console.log("Server started on port 3000");
});
//now run app.js //现在运行 app.js
1)node app.js 1)节点app.js
2)In browser, head over to "localhost:3000" 2)在浏览器中,转到“localhost:3000”
This should have solved your problem.这应该解决了你的问题。 body-parser is used when you have post request to server.
当您向服务器发送请求时使用 body-parser。 It shouldn't show any body-parser error if no post request is submitted to your server.
如果没有向您的服务器提交发布请求,它不应该显示任何正文解析器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.