简体   繁体   English

Node/Express:如何分离服务器端和客户端 javascript?

[英]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.

相关问题 如何在客户端Javascript中调用Node / Express API密钥(.env)? - How do I call a Node/Express API key (.env) in Client-side Javascript? 如何从客户端的Node and Express服务器获取完整的错误消息? - How do I get the full error message from my Node and Express server on the client side? 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)? 我如何在客户端JavaScript中要求节点模块? - How do I require a node module in my client side javascript? 在Node.js中如何与客户端JavaScript通信? - In Node.js how do I communicate with client side JavaScript? 如何使用服务器上的 Node 从客户端 JavaScript 上传到 Google Cloud Storage? - How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server? 使用 express 从节点服务器向客户端发送数据时出现错误 JavaScript - I get error when sending data using express from node server to client side JavaScript 我如何在node.js中将数组从服务器端传输到客户端? - How do I get to transfer an array from the server side to client side in node.js? 如何将服务器端ASP XmlHttpRequest代码转换为客户端JavaScript? - How do I convert my server-side ASP XmlHttpRequest code to client-side JavaScript? 如何使用 Node/Express 从客户端调用 ajax get 请求? - How do I call a invoke an ajax get request from client side, using Node/Express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM